Eclipse printing symbols instead of 'chars' on console for C - c

new to C. Trying to get it to work with Eclipse since it seems a lot easier to use then vi (formatting, syntax warnings before compiling etc).
Now my program works fine in a terminal but in Eclipse console it does not seem to work well.
I have found the source of error but I need some assistance fixing it.
For some reason Eclipse console does not like to re-locate the cursor to print over existing text (or so it seems). Because when I print regularly using printf(...) it prints fine.
I am using these functions to re-print and Eclipse console does not like it. I first re-locate the cursor with set_cur_pos(..) and then in a for loop reprint my characters on top of existing text by calling put(..)
//
// Name: put
// Description: calls putchar to place a character, then flushes stdout
// #param character - character to print onto console
void put(char character) {
putchar(character);
fflush( stdout);
} // clear
//
// Name: set_cur_pos
// Description - sets cursor at a specific location on console
// #param rCursor - row location to place cursor
// #param cCursor - col location to place cursor
//
void set_cur_pos(int rCursor, int cCursor) {
printf("\033[%d;%dH", rCursor, cCursor);
} // set_cur_pos
EG: I'd use it like this
/**
* Description: Function prints a grid using cursor control.
*/
static void printOver(char grid[40][40]) {
int i, j;
for ( i = 0; i < size; i++ ) {
for ( j = 0; j < size; j++ ) {
set_cur_pos(i, j);
put(grid[i][j]);
}
}
Any ideas why Eclipse prints this out?
[2J[0;0H [0;1H*[0;2H [0;3H [0;4H [0;5HY[0;6H*[0;7H [0;8H [0;9H [1;0H [1;1H [1;2H [1;3H [1;4H [1;5HY[1;6H [1;7H [1;8H [1;9H*[2;0H [2;1H [2;2H*[2;3H [2;4H*[2;5HY[2;6H [2;7HY[2;8H [2;9H [3;0H [3;1H [3;2H [3;3H [3;4HY[3;5HY[3;6H [3;7H [3;8H [3;9H [4;0H*[4;1H [4;2H [4;3H [4;4HY[4;5H [4;6H [4;7H [4;8H [4;9H [5;0H*[5;1HY[5;2H [5;3H [5;4H [5;5H [5;6H [5;7H [5;8H*[5;9H [6;0HY[6;1HY[6;2H [6;3H [6;4H [6;5HY[6;6HY[6;7H*[6;8H [6;9H [7;0H [7;1HY[7;2H [7;3HY[7;4H [7;5H [7;6HY[7;7H [7;8H [7;9H*[8;0HY[8;1HY[8;2H [8;3H [8;4H [8;5H [8;6H [8;7H*[8;8H [8;9H [9;0H [9;1H [9;2H [9;3HY[9;4H [9;5H*[9;6H [9;7H [9;8H [9;9H [10;0H
[0;0H [0;1H_[0;2H [0;3H [0;4H [0;5HY[0;6H_[0;7H [0;8H [0;9H [1;0H [1;1H [1;2H [1;3H [1;4H [1;5HY[1;6H [1;7H [1;8H [1;9H*[2;0H [2;1H [2;2H*[2;3H [2;4H*[2;5HY[2;6H [2;7H*[2;8H [2;9H [3;0H [3;1H [3;2H [3;3H [3;4HY[3;5H*[3;6H [3;7H [3;8H [3;9H [4;0H*[4;1H [4;2H [4;3H [4;4HY[4;5H [4;6H [4;7H [4;8H [4;9H [5;0H*[5;1HY[5;2H [5;3H [5;4H [5;5H [5;6H [5;7H [5;8H*[5;9H [6;0HY[6;1H*[6;2H [6;3H [6;4H [6;5HY[6;6H*[6;7H_[6;8H [6;9H [7;0H [7;1HY[7;2H [7;3HY[7;4H [7;5H [7;6HY[7;7H [7;8H [7;9H_[8;0HY[8;1HY[8;2H [8;3H [8;4H [8;5H [8;6H [8;7H_[8;8H [8;9H [9;0H [9;1H [9;2H [9;3HY[9;4H [9;5H*[9;6H [9;7H [9;8H [9;9H [10;0H

These \033[... escape sequences are not terminal portable, eg. between bash and cmd.
So I think the eclipse IDE console does not interpret them as a cursor set command.
The single one portable cursor command without using PALs is '\r', wich will set
the cursor to the beginning of the line.

Related

Print utf8 wchar_t in windows console

I'm running CodeBlocks in Windows 10 and even after following some of the indications found on SO, I did not manage to print in cosole utf8 chars.
_setmode(_fileno(stdout), 0x00020000); // _O_U16TEXT
setlocale(LC_ALL, "C.UTF-8");
wchar_t star = 0x2605;
wchar_t st = L'★';
if (printf("%lc\n", star) < 0) {
perror("printf");
}
if (printf("\n%lc\n", st) < 0) {
perror("printf");
}
gives the output
printf: Invalid argument
printf: Invalid argument
The console Properties >> Font is set to Lucida Console, similar to codeblocks code editor. I am able to see the star as a symbol inside my text editor but not in the console. What should I do?
EDIT:
even if I change the previous code to:
if (wprintf(L"%lc", star) < 0) {
perror("printf");
}
printf("\n");
if (wprintf(L"%lc", st) < 0) {
perror("printf");
}
I am not able to see more than two rectangles instead of the stars (which if I copy and try to paste them here, are drawn in the right way)
After trying all of my available fonts in the console, the only one which let me see the actual star is SimSun-ExtB.

Input text in graphics in C programming

I am making a project in C. Its simple, just a Hangman Game.
Got the logic already cause I've done that only in console.
Now, I'm trying to do it in C again with GRAPHICS. I am using Turbo C.
I've read some of the functions of graphics.h: so far I've seen outtext() / outtextxy() something like that. It can print a string.
Can you input a char or string in graphics? Searched a lot but seen nothing.
Seen only drawing shapes examples.
How do you input characters, integers etc. in the graphics mode?
From memory, while you can use regular stdio functions printf, scanf, and gets, the graphics driver will paint them over your screen onto a "virtual cursor" position and scroll the screen when it reaches the bottom. You can use the nonstandard conio.h functions such as gotoxy and attempt to position the cursor, but it's still a mediocre way of inputting text, messing up the graphics stuff. You also cannot use the fancy fonts!
So use getch to read characters without showing them; update a string buffer (manually handling special keys such as Backspace and Return), and draw that on the screen using a font of your choice.
A short sample snippet of code to get you started:
#define MAX_INPUT_LEN 80
char inputbuf[MAX_INPUT_LEN];
int input_pos = 0;
then, in your main loop
int the_end = 0;
do
{
outtextxy (0,0, inputbuf);
c = getch();
switch (c)
{
case 8: /* backspace */
if (input_pos)
{
input_pos--;
inputbuf[input_pos] = 0;
}
break;
case 13: /* return */
the_end = 1;
break;
case 27: /* Escape = Abort */
inputbuf[0] = 0;
the_end = 1;
break;
default:
if (input_pos < MAX_INPUT_LEN-1 && c >= ' ' && c <= '~')
{
inputbuf[input_pos] = c;
input_pos++;
inputbuf[input_pos] = 0;
}
}
} while (!the_end);
Before you draw the text, make sure to erase the previous line! I left that out because it's been too long ago I used Turbo-C.
For taking input from used.. you can use scanf function similar to how we take input in any non-graphics c program.
You can do this by using normal scanf function. And by using sprintf copy a integer/whatever variable into a string. Now you can use outtextxy to set the string in specified location using x,y axis. Check geeksforgeeks to learn sprintf syntax.
I know it is too late. Hope it helps someone else.!

Arduino Serial.println() outputs a blank line if not in loop()

I'm attempting to write a function that will pull text from different sources (Ethernet client/Serial/etc.) into a single line, then compare them and run other functions based on them. Simple..
And while this works, I am having issues when trying to call a simple Serial.println() from a function OTHER than loop().
So far, I have around 140 lines of code, but here's a trimmed down version of the portion that's causing me problems:
boolean fileTerm;
setup() {
fileTerm = false;
}
loop() {
char character;
String content="";
while (Serial.available()) {
character = Serial.read();
content.concat(character);
delay(1);
}
if (content != "") {
Serial.println("> " + content);
/** Error from Serial command string.
* 0 = No error
* 1 = Invalid command
*/
int err = testInput(content);
}
int testInput(String content) {
if (content == "term") {
fileTerm = true;
Serial.println("Starting Terminal Mode");
return 0;
}
if (content == "exit" && fileTerm == true) {
fileTerm = false;
Serial.println("Exiting Terminal Mode");
return 0;
}
return 1;
}
(full source at http://pastebin.com/prEuBaRJ)
So the point is to catch the "term" command and enter some sort of filesystem terminal mode (eventually to access and manipulate files on the SD card). The "exit" command will leave the terminal mode.
However, whenever I actually compile and type these commands with others into the Serial monitor, I see:
> hello
> term
> test for index.html
> exit
> test
> foo
> etc...
I figure the function is catching those reserved terms and actually processing them properly, but for whatever reason, is not sending the desired responses over the Serial bus.
Just for the sake of proper syntax, I am also declaring the testInput() function in a separate header, though I would doubt this has any bearing on whether or not this particular error would occur.
Any explainable reason for this?
Thanks.
Model: Arduino Uno R3, IDE version: 1.0.4, though this behavior also happened on v1.0.5 in some instances..
It is kinda guessable how you ended up putting delay(1) in your code, that was a workaround for a bug in your code. But you didn't solve it properly. What you probably saw was that your code was too eager to process the command, before you were done typing it. So you slowed it down.
But that wasn't the right fix, what you really want to do is wait for the entire command to be typed. Until you press the Enter key on your keyboard.
Which is the bug in your code right now, the content variable doesn't just contain "term", it also contains the character that was generated by your terminal's Enter key. Which is why you don't get a match.
So fix your code, add a test to check that you got the Enter key character. And then process the command.

wprintw: In ncurses, when writing a newline-terminated line of exactly the same width as the window, two newlines are printed

I've just finished working through the code of a CLI program, converting it into a TUI program using ncurses.
It tests the user on a collection of questions and answers in a flash card-like way.
All went relatively smoothly except that I have replaced many printf() calls with a popupinfo(int colour,char * title, char * body) function to pop up a window.
This function uses these functions:
int textwidth (char * text);//returns the width of a given string (which may include newlines) in chars when displayed without wrapping (for purposes of determining optimum window width)
int textheight (char * text, int width);//returns the height of a given string (which may include newlines) in lines when displayed wrapped to the given width (for purposes of determining optimum window width)
to calculate the size of the window before using wprintw() to print to that window.
The problem I have is that when the length of a line other than the last line is exactly equal to the window width (or a multiple of the window width), one or more lines of text will be omitted from the window.
For example:
Answer:
Foobarbaz.
will print correctly, but in:
Answer:
Foo.
The 'Foo.' is not printed.
I believe this is because the wprintw() function moves the cursor to a new line after printing (window_width) chars, but then encounters the newline character that was at the end of the line it just printed as well.
Does anyone know of a way (short of writing an entire function to handle output myself) to stop this happening?
Useful details:
I'm replacing:
printf("\nSorry, the correct answer is:\n\n\t%s\n\n",currententry->answer);
with:
sprintf(passingstring,"The correct answer is:\n\n%s",currententry->answer);
popupinfo(3,"Sorry!",passingstring);
popupinfo is defined as:
void popupinfo(int colour,char * title,char * message)//pops up a window with the given colour, title and text
{
WINDOW * wbpopup = NULL, * wpopup = NULL;
PANEL * ppopup = NULL;
int width, height;
width=textwidth(message);
getmaxyx(stdscr,nlines,ncols);
if (width>ncols-16)width=ncols-16;
height=textheight(message,width)+4;
width+=8;
if (!(wbpopup = newwin(height,width,(nlines-height)/2,(ncols-width)/2))) outofmemory();
ppopup = new_panel(wbpopup);
wattrset(wbpopup,COLOR_PAIR(colour));
werase(wbpopup);
wbkgd(wbpopup,COLOR_PAIR(colour));
box(wbpopup,0,0);
windowtitle(wbpopup,title);
wpopup = innerwindow(wbpopup);
wprintw(wpopup,message);
update_panels();
doupdate();
wgetch(wpopup);
delwin(wpopup);
del_panel(ppopup);
delwin(wbpopup);
update_panels();
doupdate();
}
Also useful:
int textwidth (char * text)//returns the width of a given string (which may include newlines) in chars when displayed without wrapping (for purposes of determining optimum window width)
{
int i=0,j=0,k=0;
while (text[i]!='\0')
{
if (text[i]=='\n')
{
k=j>k?j:k;
j=0;
}
else j++;
i++;
}
k=j>k?j:k;
return k;
}
and
int textheight (char * text, int width)//returns the height of a given string (which may include newlines) in lines when displayed wrapped to the given width (for purposes of determining optimum window width)
{
int i=0,j=0,k=1;
while (text[i]!='\0')
{
if (text[i]=='\n')
{
k++;
j=0;
}
else j++;
if (j>width)
{
k++;
j=1;
}
i++;
}
return k;
}
Other functions:
WINDOW * innerwindow(WINDOW * outerwindow);//creates an area within another window for purposes of displaying text with a margin
void windowtitle(WINDOW * window, char * title);//writes the given string to the given window (top centre)
For anything further, see full source for the CLI and ncurses versions, which can be found at http://github.com/megamasha
You are absolutely right when you say:
I believe this is because the wprintw() function moves the cursor to a
new line after printing (window_width) chars, but then encounters the
newline character that was at the end of the line it just printed as
well.
Regarding your question
Does anyone know of a way (short of writing an entire function to
handle output myself) to stop this happening?
- there is no such way, because what you observe is how line wrapping works in ncurses.
What you could do is making the popup window one character wider, thus avoiding the wrapping that is due to reaching the width of the window, for example by changing the line width+=8; to width+=8+1; in popupinfo.

erasing terminal output on linux

I was writing a command line program which will have a status bar, much like wget.
The main problem I'm facing is: how do I delete what I've already sent into stdout/stderr?
I had on idea: use the backspace char '\b' and erase the output I've sent. Is that the best way? Is it the only way? Is there a better way?
PS: I don't want to use anything like ncurses. Plain old C please.
Thanks
EDIT:
Can I also go up and/or down? Example: I have 10 lines of output, I want to change the 3rd line from Doing ABC to ABC: Done. How can I do that?
Also, can anyone post more details about what VT102 characters are? What are its capabilities? Please post good links on this if you have any.
Thanks
The basic formatting control characters are backspace (\b), tab (\t), newline (\n), and carriage return (\r). If you need more than that then you can use ANSI X3.64 / ISO/IEC 6429 / ECMA-48 escape sequences; at least the VT100 subset is recognized by most modern terminals and emulators. An advantage of using ncurses is that it will look up the capabilities of your particular terminal and so it will work even if your terminal uses a different set of escape sequences.
You have to remember that as far as the regular stdio routines are concerned, stdout is just a byte stream with no inherent display characteristics; that depends on the target device, which can be anything from a regular VT100-style terminal to a hardcopy terminal to a sheet-fed printer to a plotter to whatever.
IMO, you're far better off using a library like ncurses than trying to hack together your own display management code with VT100 escape codes, even for a relatively simple task like this. I know you want to stick with "plain old C", but this is a task that falls outside the bounds of plain old C.
Use '\r' to return to the beginning of the line and possibly rewrite the whole line.
Look for VT102 control sequences - these are character sequences ESC ... to control your terminal.
There is also the possiblity of using Ncurses, which is a library for Textual UI, where this kind of behaviour should have some support. However, it may be overkill for something like this.
A slight variation on your own solution:
You can also print a carriage return (\r), which will return you to the start of the line.
It is a progressbar for bash.
function gauge()
{
progress="$1"
total="$2"
width=`tput cols`
let gwidth=width-7
if [ "$total" == "0" ]; then
percent=100
else
set +e
let percent=progress*100/total;
set -e
fi
set +e
let fillcount=percent*gwidth/100
let nofillcount=gwidth-fillcount
set -e
fill="";
if [ "$fillcount" -gt "0" ]; then
for i in `seq $fillcount`; do
fill="$fill""|"
done
fi;
nofill=""
if [ "$nofillcount" -gt "0" ]; then
for i in `seq $nofillcount`; do
nofill="$nofill"" ";
done
fi
echo -e -n "\r[""$fill""$nofill""] ""$percent""%";
}
About the progress bar: something like this?
#include <stdio.h>
#include <unistd.h>
typedef enum
{
false=0,
true=!false
} bool;
typedef struct
{
/* Start delimiter (e.g. [ )*/
char StartDelimiter;
/* End Delimiter (e.g. ] )*/
char EndDelimiter;
/* Central block (e.g. = )*/
char Block;
/* Last block (e.g. > ) */
char CurBlock;
/* Width of the progress bar (in characters) */
unsigned int Width;
/* Maximum value of the progress bar */
double Max;
/* True if we have to print also the percentage of the operation */
bool PrintPercentage;
/* True if the bar must be redrawn;
note that this must be just set to false before the first call, the function then will change it by itself. */
bool Update;
} ProgressBarSettings;
/* Prints/updates the progress bar */
void PrintProgressBar(double Pos, ProgressBarSettings * Settings);
/* Inits the settings of the progress bar to the default values */
void DefaultProgressBar(ProgressBarSettings * Settings);
int main()
{
int i;
/* Init the bar settings */
ProgressBarSettings pbs;
DefaultProgressBar(&pbs);
pbs.Max=200;
pbs.Width=60;
printf("Progress: ");
/* Show the empty bar */
PrintProgressBar(0,&pbs);
for(i=0;i<=pbs.Max;i++)
{
/* Wait 50 msec */
usleep(50000);
/* Update the progress bar */
PrintProgressBar(i,&pbs);
}
puts(" Done");
return 0;
}
/* Inits the settings of the progress bar to the default values */
void DefaultProgressBar(ProgressBarSettings * Settings)
{
Settings->StartDelimiter='[';
Settings->EndDelimiter=']';
Settings->Block='=';
Settings->CurBlock='>';
Settings->PrintPercentage=true;
Settings->Update=false;
Settings->Max=100;
Settings->Width=40;
}
/* Prints/updates the progress bar */
void PrintProgressBar(double Pos, ProgressBarSettings * Settings)
{
/* Blocks to print */
unsigned int printBlocks=(unsigned int)(Settings->Width*Pos/Settings->Max);
/* Counter */
unsigned int counter;
/* If we are updating an existing bar...*/
if(Settings->Update)
{
/* ... we get back to its first character to rewrite it... */
for(counter=Settings->Width+2+(Settings->PrintPercentage?5:0);counter;counter--)
putchar('\b');
}
else
Settings->Update=true; /* next time we'll be updating it */
/* Print the first delimiter */
putchar(Settings->StartDelimiter);
/* Reset the counter */
counter=Settings->Width;
/* Print all the blocks except the last; in the meantime, we decrement the counter, so in the end we'll have
the number of spaces to fill the bar */
for(;printBlocks>1;printBlocks--,counter--)
putchar(Settings->Block);
/* Print the last block; if the operation ended, use the normal block, otherwise the one for the last block */
putchar((Settings->Max==Pos)?Settings->Block:Settings->CurBlock);
/* Another block was printed, decrement the counter */
counter--;
/* Fill the rest of the bar with spaces */
for(;counter;counter--)
putchar(' ');
/* Print the end delimiter */
putchar(Settings->EndDelimiter);
/* If asked, print also the percentage */
if(Settings->PrintPercentage)
printf(" %3d%%",(int)(100*Pos/Settings->Max));
/* Flush the output buffer */
fflush(stdout);
};
Note: the unistd.h and usleep thing is just to fake the progress of an operation, the progress bar code itself just uses the standard library. Its only assumptions about the output stream are that \b actually gets to the previous written character. I tried it successfully on Windows and Linux (with gnome-terminal), don't know if it doesn't work correctly with some terminal emulators.
Sorry for the excessive amount of comments, I wrote it for another forum where I needed to explain pratically every line of the code to a C newbie.

Resources