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

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..

Related

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.

Writing to file multiple times in c [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 4 years ago.
I have a simple C program that counts how many seconds has passed then writes that number to a File. Basic stuff, but every time I run it the previous number let’s say 15 seconds is replaced with the new one. How do I go about being able to run that program whenever and it records all the instances of those seconds and not just delete the previous and insert a new. Basically I’m recording static dry breath holds for free diving and want to see the progress in a week/month/etc. So I need the data written to the file the first day to stay there. Thanks guys
You need to open the file in append mode. This way your file won't be rewritten each time but instead new content will be added to the end of the file.
Let's say that you have this:
FILE *stackoverflow;
stackoverflow=fopen("myfile.dat", "a"); /* here opening file in "a" will resolve the issue */
if(stackoverflow==NULL) {
perror("Error opening file.");
}
else {
/* do as you wish so */
fclose(stackoverflow);
}

How do i read a file from bottom to top and right to left? [duplicate]

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.

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

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.

Resources