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

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.

Related

COBOL Replace the first line in a file without using OPEN I-O and REWRITE

Say that I have a file with the below format
<records count="n">
record line 1
record line 2
.
.
.
record line n
</records>
I'll have to open this file and change the value of n to another value based on some logic. After change my file should look like.
<records count="m">
record line 1
record line 2
.
.
.
record line n
</records>
I can open the file in OPEN I-O mode and change the first line using the REWRITE option to replace the first line. But I don't want to use these methods. Is there a way to achieve the same logic using OPEN INPUT and OPEN OUTPUT mode and replace the line with WRITE method.
Is there a way to achieve the same logic using OPEN INPUT and OPEN
OUTPUT mode and replace the line with WRITE method[?]
No, that would leave you with only the <records count="m"> in the file. All other records would be lost!
As long as the length of the first record is the same, after changing n to m, REWRITE is the most straight forward way to update that record.
Perhaps, if you explain why you want to use WRITE, there may be something else that could be done.
If the file is not 'too' large, read all the records into memory, change the first record, then write all the records to the file.
If the file is 'too' large, copy the file changing the first record, delete the first file, then rename the copy.
Perhaps less efficient for 'too' large, sort the file by adding a sequence number and changing the first record. This simply uses the sort file to hold the data, temporarily. Possibly a poor choice for a program to be converted.
You need to define what the limit for 'too' is.
There are non-standard routines for file access in Micro Focus, but those might be more difficult to convert.

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 a specific line from text file without reading whole file in C [duplicate]

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

C - dynamically modifying a file - is it possible?

I'm writing a small program in C and I want to have the option of saving data to file and then reading it from that file. The data is BIG, so I want to somehow dynamically write to a file without having to create a new file and copy modified old file into it.
Here's exactly what I want to do:
In the first line, I want to have "description" of the data in the form "%s %s %s ... %s \n" where %s is a string and the n'th string describes data in n+1'th line. I want to read the 1'st line of the file, scan for corresponding "description" string, and if it is not present, append it to the first line, and the data corresponding to it after the last line of the file.
The question is - is it possible to "jump" into lines in the file without scanning all the previous lines, and can I somehow read the first line of the file and append something to it after reading? Or maybe it is not the way to go in this situation and C offers some kind of different solution?
What you want can be done using stdio and fseek(). As long as you know at what byte offset you want to go, you can overwrite and/or append anywhere in the file without reading the data before, or the data you're overwriting. What you can not easily do is insert data, i.e., open the file, split it in half and put data in between.
Not too sure if that is what you mean though...

How to replace a line on the middle of a txt file in C?

I am reading info (numbers) from a txt file and after that I am adding to those numbers, others I had in another file, with the same structure.
At the start of each line in the file is a number, that identifies a specific product. That code will allow me to search for the same product in the other file. In my program I have to add the other "variables" from one file to the other, and then replace it, in the same place in one of those files.
I didn't open any of those files with a or a+, I did it with r and r+ because i want to replace the information in the lines that may be in the middle of the file, and not in the end of it.
The program compiles, and runs, but when it comes to replace the info in the file, it just doesn't do anything.
How should I resolve the problem?
A program can replace (overwrite) text in the middle of the file. But the question is whether or not this should be performed.
In order to insert larger text or smaller text (and close up the gap), a new text file must be written. This is assuming the file is not fixed width. The fundamental rule is to copy all original text before the insertion to a new file. Write the new text. Finally write the remaining original text. This is a lot of work and will slow down even the simplest programs.
I suggest you design your data layout before you go any further. Also consider using a database, see my post: At what point is it worth using a database?
Your objective is to design the data to minimize duplication and data fetching.

Resources