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

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

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.

How can I search for a specific string in a csv file, and to know in which line that string was found?

I started to learn C and I am writing a code where the user needs to type a string he wants to search in the csv file , and if the string was found , it will print the number of line the string was found at .
and I am not exactly sure what is the best way to do that
You may read line by line using getline() until it returns -1 (EOF), keeping a counter on how many iterations you have done.
For each line you read, you can use strstr() to check if the string is present. If yes, just print the counter (that will correspond to the line).

How can I find and edit a line in a text file using batch?

I have a text file with multiple of lines. Each line contains a known word and an unknown number. I want to make a batch file which searches for a specific line (with a known word) and replaces the random number with a number with is entered by the user.
I already used FART but I don't know the part with the unknown number.

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