Is it possible to scan a file in reverse from the last line in C? - c

I am running a simulation and want to add an option to continue evolving from the last iteration of a previous run. In order to do so, I need to read the last 2 lines of data from a file. Is there any way to do this without using fscanf to scan from the beginning of the file?

Have the previous run record in another file the ftell() values of the last few lines and other info to note the meta data of the file (e.g. date-time-modified).
A subsequent run can use this info to begin where the prior run left off.
If this side file is missing or does not agree with the current state of things, walk the files with fgetc(), fgets(), etc. to find where to begin again.

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.

Ada: Write to File Multiple Times

In Ada, can you open, write to, close, then reopen, write to, and close a txt file without it being overwritten? Like continue from where it last left off?
Thanks!
Yes. If you look in section A.10.1 in the reference manual, you can see that the package Ada.Text_IO includes the declaration:
type File_Mode is (In_File, Out_File, Append_File);
Append_File is the mode you are looking for.
A.10.2(3) in the reference manual requires that you get a new line, when you close a file:
For the procedure Close: If the file has the current mode Out_File or Append_File, has the effect of calling New_Page, unless the current page is already terminated; then outputs a file terminator.
... where A.10.5(16) explains what New_Page does:
Operates on a file of mode Out_File or Append_File. Outputs a line terminator if the current line is not terminated, or if the current page is empty (that is, if the current column and line numbers are both equal to one). Then outputs a page terminator, which terminates the current page. Adds one to the current page number and sets the current column and line numbers to one.
If you want more detailed control over what ends up in a file, you should use one of the other I/O packages.

How to read from a specific line from a text file in VHDL

I am doing a program in VHDL to read and write data. My program has to read data from a line, process it, and then save the new value in the old position. My code is somewhat like:
WRITE_FILE: process (CLK)
variable VEC_LINE : line;
file VEC_FILE : text is out "results";
begin
if CLK='0' then
write (VEC_LINE, OUT_DATA);
writeline (VEC_FILE, VEC_LINE);
end if;
end process WRITE_FILE;
If I want to read line 15, how can I specify that? Then I want to clear line 15 and have to write a new data there. The LINE is of access type, will it accept integer values?
Russell's answer - using two files - is the answer.
There isn't a good way to find the 15th line (seek) but for VHDL's purpose, reading and discarding the first 14 lines is perfectly adequate. Just wrap it in a procedure named "seek" and carry on!
If you're on the 17th line already, you can't seek backwards, or rewind to the beginning. What you can do is flush the output file (save the open line, copy the rest of the input file to it, close both files and reopen them. Naturally, this requires VHDL-93 not VHDL-87 syntax for file operations). Just wrap that in a procedure called "rewind", and carry on!
Keep track of the current line number, and now you can seek to line 15, wherever you are.
It's not pretty and it's not fast, but it'll work just fine. And that's good enough for VHDL's purposes.
In other words you can write a text editor in VHDL if you must, (ignoring the problem of interactive input, though reading stdin should work) but there are much better languages for the job. One of them even looks a lot like an object-oriented VHDL...
Use 2 files, an input file and an output file.
file_open(vectors, "stimulus/input_vectors.txt", read_mode);
file_open(results, "stimulus/output_results.txt", write_mode);
while not endfile(vectors) loop
readline(vectors, iline);
read(iline, a_in);
etc for all your input data...
write(oline, <output data>
end loop;
file_close(vectors);
file_close(results);

How to remove all lines, line by line from a file?

I'm running a loop that takes names from a file.
What I want to do is when the name is taken, it then deletes it from the file. How can I do this?
Try out fileinput.
It goes something like:
import fileinput
for line in fileinput.input(someFileName, inplace=True):
doSomething()
Any line iterated over is consumed, hence deleted from the file.
If you need to keep it just print it, it'll be rewritten to the file in the same location.
i.e. if you won't print the line back it'll disappear :)
Native files don't work that way. You'd have to rewrite the entire list of remaining files, each time through the loop. Performance will be bad (unless the list of files is always very short). And the failure modes are very bad if your process crashes while you're trying to update the file because you can't do the update in place. The best you could do is: write to a temp file, delete the original file, then rename (or move) the temp file.
Instead, you should consider using SQLite. You could either delete records from a File table, or have a status field that tracks which files have been processed.

C Remove the first line from a text file without rewriting file

I've got a service which runs all the time and also keeps a log file. It basically adds new lines to the log file every few seconds. I'm written a small file which reads these lines and then parses them to various actions. The question I have is how can I delete the lines which I have already parsed from the log file without disrupting the writing of the log file by the service?
Usually when I need to delete a line in a file then I open the original one and a temporary one and then I just write all the lines to the temp file except the original which I want to delete. Obviously this method will not word here.
So how do I go about deleting them ?
In most commonly used file systems you can't delete a line from the beginning of a file without rewriting the entire file. I'd suggest instead of one large file, use lots of small files and rotate them for example once per day. The old files are deleted when you no longer need them.
Can't be done, unfortunately, without rewriting the file, either in-place or as a separate file.
One thing you may want to look at is to maintain a pointer in another file, specifying the position of the first unprocessed line.
Then your process simply opens the file and seeks to that location, processes some lines, then updates the pointer.
You'll still need to roll over the files at some point lest they continue to grow forever.
I'm not sure, but I'm thinking in this way:
New Line is a char, so you must delete chars for that line + New Line char
By the way, "moving" all characters back (to overwrite the old line), is like copying each character in a different position, and removing them from their old position
So no, I don't think you can just delete a line, you should rewrite all the file.
You can't, that just isn't how files work.
It sounds like you need some sort of message logging service / library that your program could connect to in order to log messages, which could then hide the underlying details of file opening / closing etc.
If each log line has a unique identifier (or even just line number), you could simply store in your log-parsing the identifier until which you got parsing. That way you don't have to change anything in the log file.
If the log file then starts to get too big, you could switch to a new one each day (for example).

Resources