get file cursor position in C - 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.

Related

How to get a specific line on a text file directly in C? (without iterating line-by-line)

Is there a way to get a specific line inside a text file without iterating line-by-line in C?
for example I have this text file names.txt it contains the following names below;
John
James
Julia
Jasmine
and I want to access 'Julia' right away without iterating through 'John' and 'James'?, Something like, just give the index value of '2' or '3' to access 'Julia' right away.
Is there a way to do this in C?
I just want to know how because I want to deal with a very large text file something like in about 3 billion lines and I want to access a specific line in there right away and iterating line-by-line is very slow
You have to at least once iterate thru all lines. In this iteration, before reading a line, you record the position in the file and save it to an array or to another file (Usually named an index file). The file shall have a fixed record size that is good for storing the position off the line in the text file.
Later, when you want to access a give line, you either use the array to get the position (Line number is the array index) or the file (You seek into the file to offset line number of record size) and read the position. Once you get the position, you can see into the text file to that position and read the line.
Each time the text file is updated, you must reconstruct the array or index file.
There are other way to do that, but you need to better explain the context.

How to scan one part of a file as a variable

I'm a beginner and I have a code that requires me to scan the last number of a file into the variable a.
The file looks something like this:
car,4,50,800
If I want to scan the "800" into the variable a, how do I do that?
In order to do it you have to read the data first and find the last occurance of the ',' char. Then you can get the data till let's say the end of file (EOF) or the new line char (\n). 8t all depends by the protocol you are using to store your data.

GetConsoleScreenBufferInfo for length of a single console line

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.

C : Extracting sentences extended to next line

I have a file in which on each line there are multiple sentences separated by white spaces. SOmetimes one sentence may extend to next line. I want to extract these sentences separated by white space. My code successfully extracts sentences on same line separated by white space but since it is reading line by line. SO, issue comes when one sentence is extended to next line.
Store the part unused in creation of line at each iteration in temperary buffer. Include the buffer in the next iteration (append at the begining of line read).

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

Resources