Rewrite to a specific line in c

I have a file of about 2000 lines of text that I create in my program, each line has information about the employee and is displayed as follows

1 1 Isaac Fonseca 58 c 1600 1310.40 6 1 0.22 2164.80 1
2 1 Manuel Gutierrez 22 d 1700 1523.37 4 1 0.13 897.26 1
3 1 Daniel Bernal 34 s 1600 1195.84 2 1 0.26 836.16 1
4 1 Miguel Gonzalez 43 e 1800 1195.84 0 1 0.15 0.00 1

But whenever I edit employee information, I need to update the file, what I do, I look for the line and try to rewrite it

I saw the following question about someone with the same problem, but when I try to write to a file, it always writes to the end of the file

rewriting a specific line in a text file?

Here is my code:

datos = fopen(archivo,"a+"); for(i=0;i<num;i++){ // buscar la linea fgets(lineaA,100,datos); // sobreescribir if(i == (num-1)) cursor = ftell(datos); } cursor -= strlen(lineaA) - 1; fseek(datos,cursor,SEEK_CUR); fputs(linea2,datos); fclose(datos); 
+2
c file fseek fputs ftell
Jul 14 2018-12-12T00:
source share
3 answers

You largely cannot do this.

You can use fseek to go to a specific place in the file, and you can write data to that place (you probably want to use the "r+" mode).

But a file is usually stored as a sequence of bytes (or characters), and not as a sequence of strings.

If you carefully write down exactly the same number of bytes that were already in the string, you can probably leave with it, but in your example the lines have different lengths, so this is not an option.

You can, for example, pad each line with spaces, so that they have the same length, but then it’s easy to spoil the layout if you change the file using, say, a regular text editor.

For text files, the usual approach is to read the file into memory, change the representation in memory, and recreate the entire file. You probably want to write the contents to a new temporary file, and then rename it after you confirm that there were no write errors.

This is what text editors usually do.

(Don’t worry about efficiency, 2000 lines are not so many, and you won’t notice how long it takes to write them.)

There are alternatives. You can define a binary format with fixed records or you can use a database. Or you can simply add new lines to the file and treat duplicates as if they were deleted (but this can lead to the file being very large if you make a lot of updates).

+3
Jul 14 '12 at 22:45
source share

If this is not something like homework, when you need to flip all the code yourself, perhaps the easiest way is to go to the database. Given that the rest of your code is working for you, SQLite will probably be the most obvious choice.

If you really want to stick (something at least similar) to your current format and code, I would probably write data with each line filled with something like 64 or 128 bytes (and if you have multiple entries that last longer, isolate them, for example, move all of them to the end of the file or put in a separate file). This way (most?) Editing can be done without affecting other lines. If you have a line pointer for an employee, you can also directly look for that line, rather than reading all of its predecessors to find it.

+1
Jul 14 '12 at 10:50
source share

You open the file with the parameter "a +", which allows you to add to the file, rather than overwrite. Do you want to use "r +"

 datos = fopen(archivo,"r+); 
0
Jul 14 '12 at 22:44
source share



All Articles