GetConsoleScreenBufferInfo for length of a single console line - c

I have seen here that is possible to get the length of all output to the console at a given time, however I am wanting to get the length of an individual line in the console (i.e. at a specific COORD). Is this possible with Win32 API?

Use GetConsoleScreenBufferInfo() to get the width of the console screen buffer. Next do for every position from width to 0 in the line you are interested in ReadConsoleOutput() and check if the character at the position is a whitespace character (isspace()). If it is not you have found the position of the last character in the line and its X-coordinate is the lenght of the line.

Related

Clearing input from C program

Is there a way to either;
(1) Clear any input from the terminal
(2) take input without the input being saved to the terminal
For example, in this program I enter a string and it checks if any of the letters are the same. Ideally I would want the input string to be cleared so that the hashtags form a rectangle by the 5th guess. I am aware that clearing the entire terminal and reprinting all the rows is an option but I would much rather just clear the line of input after every guess (if this is possible).
You can use VT100 escape codes. Most terminals, including xterm, are VT100 aware. For erasing a line, this is ^[[2K. In C this gives:
printf("\33[2K\r");
Some worthwhile subtleties...
\33[2K erases the entire line your cursor is currently on
\033[A moves your cursor up one line, but in the same column i.e. not to the start of the line
\r brings your cursor to the beginning of the line (r is for carriage return N.B. carriage returns do not include a newline so cursor remains on the same line) but does not erase anything

Comparing a string from text file and then printing out the entire line(s)

My goal is to print out every full line from a text file if that line contains a string that is equivalent to user input.
I understand how to find the occurrences of a specific string in a text file, but I am confused as to how to associate that with a specific line. How do I relate my string with the specific line that it is in?
My initial thought was to store each line in an array and then print out that line if the user string is somewhere in that line.
However each line is a different size, so I was wondering if it is possible for me to initially divide my entire text file into x number of lines and then use a loop to go through each line and search for that string?
Save the file pointer of the starting of the line in a temp variable before starting new line compare

How do I display text in a gtk widget without using a textbox?

Here is my current application
http://imgur.com/JhNMv
As you can see from the output, I am using pdcurses. In order to achieve the GPU 0 and GPU 1 lines, I use mvwprintw which allows me to specify a variable and add +1 so that it prints on the next line.
Sadly, in GTK, all I have is GTKtextview and GTKtreeview. That is not what I need. I need to print the same way gtk_label_set_text works but without overwriting the line, and printing on the next
If I understand what you're asking, you want a GtkLabel with multiple lines of text - you can do that by just setting the label to a string with newlines in it. If you want to append a line to an existing label, first grab the existing text with gtk_label_get_text(), copy it and append a newline character and the next line.

How to go to the previous line in a C code

If for the following code:
printf("HEllo\n"); // do not change this line.
printf("\b\bworld");
I need an output: Helloworld (In a single line). But this does not work fine. Could anyone please explain the reason? And other escape sequence if any.
There is no platform-independent control character to move back up a line. This harkens back to the days of line printers, where printf actually would print a line of text onto a sheet of paper and there was no way of retracting the paper to overwrite what had already been printed.
That said, there are libraries like ncurses that let you move the cursor around the console. They're just not part of the standard libraries.
How about simply:
printf("Helloworld");
\n is an escape sequence for a new line. Since you want everything to appear on the same line, there's no reason to specify \n.
The problem is you can't reliably move back up a line (using \b) after you've printed a new line. But if you require that there be two lines of code in your source, you can simply omit both escape sequences:
printf("HEllo");
printf("world");
If you're writing a Win32 console application, you can take advantage of the Console Screen Buffer API. The following code will move 1 line up:
printf("HEllo\n");
CONSOLE_SCREEN_BUFFER_INFO coninfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &coninfo);
coninfo.dwCursorPosition.Y -= 1; // move up one line
coninfo.dwCursorPosition.X += 5; // move to the right the length of the word
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);
printf("world");
Output:
HElloworld
Remove "\n" from your first printf. It moves the cursor to a new line.
Here is the list of escape sequences.
If you can't remove "\n", then you can do make a copy of a substring without these charaters. See the following example:
const char* from = "12345678";
char *to = (char*) malloc(6);
strncpy(to, from+2, 5);
All you need is to determine the index of "\n" characters.
The backspace character, when sent to a stream (such as through the printf() family of functions), does not seek backward in the file, it is sent as-is. If you run your example, the backspace character will be output as "garbage".
If you don't want a new line, don't post a newline character.
It is because '\b' is a terminal escape code... (sans the 'escape' of course ;-)
so it only modifies what you see on the terminal. That is, the terminal adjusts its display to respond to the backspace code, even though it received everything prior to it. A file also receives everything, including the backspace code, but it is not a tty device so the file's content is not modified; it keeps everything you send to it.
If printing an extra blank is a problem, then you should code some extra logic to print the trailing blank on every output except the last.
Reference This is in reference to files but maybe same idea applies. You might want to check out this link there is a very detailed explanation that most probably answers your question.
In a console, you can't go up a line. You can clear the screen and redraw it (which simulate going up a line.) Or you can rewrite on the same line. \r will take you to the beginning of the line you just printed.
You can do it actually in a platform-independent* way, assuming that you can somehow calculate the x offset of the previous line.
int x = printf("Hello, World!\n");
gotoxy(x-1-1,gety()-1); // One for the length-offset difference and the other to skip \n
printf("\b \b");
You can avoid using that variable by directly replacing it with x.
Note: printf() returns an int (the length of the passed String (of characters)). Use it wisely :)

get file cursor position in C

I am reading a file with fgetc, so each time it reads a character, the cursor positio gets changed.
Is it possible to know, after each read, the "coordinates" of the cursor on the file in terms of column and line number?
Thanks
You can use ftell
It does not give you the position in terms of row and column but gives the current position in the stream from the start.
There is no "coordinates" in a file, only a position. A text file is simply a stream of bytes, and lines are separated by line breaks. So, when reading a text file you can calculate your "coordinates" if you scan the whole file. This means, if you really need some "row" and "column" value:
Read the file line by line. Count the newline characters, and you get the "row" number. Be aware that there are different line break characters on different OS -- unix line endings are different to Windows.
Read the line in question character by character and count the characters to the position in question. This will get you the "column" number. You obviously need to accept that the number of "columns" can vary between "rows", and it's perfectly possible to have "rows" with a "column count" of 0.
A different approach would be to
Read the file line by line and build an array of the position (using ftell) of the line breaks.
Now to figure the position of any character just get its position in the file, then find the nearest previous line break. From the line break count up to the character you get the "row", from the difference between the line break position and the current position you get the "column".
But most important is to accept that there is no rows or columns in files -- there's a position in a file, but the file itself is simply a stream of bytes. This also means that you would need to handle files encoded with wide character sets differently, as a character doesn't map to a byte anymore.

Resources