Read a specific line from text file without reading whole file in C [duplicate] - c

This question already has answers here:
How to fgets() a specific line from a file in C?
(5 answers)
Closed 7 years ago.
I want to read a specific line from a text file without reading the whole file line by line. For Example, if I have 10 lines in a text file and I have to read 6th line, I will not read the first 5 lines but will directly read the 6th one. Can anyone help me??

This question is answered here
Quoting from above,
Unless you know something more about the file, you can't access specific lines at random. New lines are delimited by the presence of line end characters and they can, in general, occur anywhere. Text files do not come with a map or index that would allow you to skip to the nth line.
If you knew that, say, every line in the file was the same length, then you could use random access to jump to a particular line. Without extra knowledge of this sort you simply have no choice but to iterate through the entire file until you reach your desired line.
Credits : Quoted answered was by David Heffernan

You could 'index' the file. Please note that this is only worth the effort if your text file:
is big
is frequently read and rarely written
The easiest (and probably most efficient) way is to use a database engine. Just store your file in a table, one row for each line.
Alternatively, you could make your own indexing mechanism. Basically, this means:
create a new file (the index)
scan the entire text file once, storing the offset of each line in the index file
repeat the above each time the text file changes
Finding line n in the text file requires two seeks:
read the nth offset from the index
read a line from the text file, starting at the offset found in the index

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 move the cursor to write a new line in a file without having to read the whole existing line? [duplicate]

This question already has answers here:
Append to the end of a file in C
(2 answers)
Closed 3 years ago.
I'm trying to write a log file to my code but I have some restrictions and don't know how to surpass them.
My code runs for several days and loops every 1 minute. I want to write in the log file at the end of every loop, so the log file will have thousands of lines. So, my two main points about this are:
I would like to be able to open and close the file at every loop (after I finish the operations, I open the file, write what I want and then close it). This way I can open the log file anytime to check how the code is going.
Each line of the log file will have a different length depending of what happened in the loop. Since the file will have thousands of lines, I would like to be able to go to the next line without having to read all the previous existing lines.
I've tried to use the fseek function like this:
fseek(fp,-1,SEEK_END);
but had no success (I ended up writing over the already existing line).
It's important to say that I'm writing this code in linux but would like it to be portable.
Everything I found here on other questions shows people reading the whole line and I don't need to read or store the existing lines.
I just to want to open the file and write in a new line. Does anyone know how I can do this?
Open the file in append mode ("a" in fopen). That way all writes will go to the end of the file; no seeking required.
Also, there is no point in opening/closing the same file repeatedly. Just open the file once, before the loop starts. Keeping a file open does not prevent others from reading it. If you're concerned with delays caused by buffering, you can just fflush() the handle after every line of output.

Reading the text file every time or Get content and store it with dynamic array in C Programming, which is best idea?

I have the text file with 2000 lines and each line contains up to 256 characters.
I need to detect EOF as I'm reading the file.
Should the code process a line as it is read or should it read the lines into an array and then process the array one element at a time?
If the data don't change in your file, you should stock the data in an array.
In the other case, you have to read your file each time you need to check a line.

Read from last line in file to first in C [duplicate]

This question already has answers here:
Reading a text file backwards in C
(5 answers)
Closed 8 years ago.
I am trying to implement a shell in C. Here's the thing. I want to make a history friendly function where if I press up it goes to prev. command.
Now I have a file which stores the history, say history.txt. When I execute a command, I would append the command to the text. And resets an offset of some sort to the last line of the file.
I need a way to find the last line and move up a line one by one on command. AND move up one by one on command.
Right now, an idea I have is to fgets() till -1 or something?
Any ideas for how I should start?
edit: I can think of a solution using an Array. But is there a way where I use little to no space?
Don't bother reading history from the file when you need to run the previous command. Just store the previous commands in memory. Write them to disk on exit, and load them on startup. That's sort of how real shells work.

How to delete the first line (not overwritten) of a file in C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Delete a Line from a file in C Language
I know we could overwrite the first line, but here i need to total remove it.
For example, the file contains 100 lines, after i remove the first line, there are 99 lines left and the file size if reduced.
Note that the file is pretty large.
I don't know how to start this, is there any suggestion ? many thanks !
collect all the text of your file into a buffer and copy the same back into yoour file and whenever the required line appears, skip it.. This is all you can do..

Resources