I have a file of about 2000 lines of text that i generate in my program, every line has the information of an employee and it's outputed like this
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 c 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 i whenever i edit an employee information i have to update the file, what i'm doing it's i search for the line and try to rewrite it
I've seen the following question of someone with the same problem, but when i try to write to the file it always writes to the end of file
overwriting a specific line on 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);
a+
opens for append, which is not what you want. Mayber+
, or maybe you have to use a different open command. But I've never tried to do it. (Do note that you need to overwrite with EXACTLY the same number of bytes as the original line.) – Trentontrepanr+
and it did append to a specific line in the code, now i just need to recalculate my cursor position, and make it delete the other content, because it just appends it, it doesn't overwrite it – Picasso