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.
Related
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.
This question already has answers here:
Printing lines from a text file in reverse order
(2 answers)
Closed 5 years ago.
Im trying to read a file not just from the last line but from the end of the line to the beginning too. Example:
Im trying
to read a file
How do I want to read:
elif a daer to
gniyrt mI
How do I do that?
You read each line, put all in a buffer ..reverse it and then parse.
The thing is most of the time it is not needed to read the file in reversed manner..But you can try this.
Some nice organized manner of doing this will be..using a LIFO data structure.
You can use a simple array and make it behave like a stack. Push everything then pop from top.
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
This question already has answers here:
Problem redirecting a C program output in bash
(5 answers)
Closed 9 years ago.
So this is probably a stupid question, but I can't see what I'm doing wrong.
I am running a program that produces output when called like ./ar. The output looks like:
-0.00781 0.02344 0.98828
-0.01172 0.02734 0.98828
-0.01562 0.02344 0.98047
-0.00781 0.02344 1.00000
-0.00391 0.02344 0.98438
A new line of output is written every second.
When I call the code like this ./ar > log and kill the program using ctrl-c after a few seconds, the file log is empty.
I am running this code on an embedded system. The system has a writeable partition which is the partition that I am running in, and I have write access as I am logged in as root.
The reason is the lazy writing concept of UNIX system.
Are you sure you are looking at standard output in you call ./ar? It might be standard error.
So, try ./ar >log 2>err to have 2 files, one for stdout and one for stderr.
Or use ./ar 2>&1 >log to get one file for both streams.
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..