how to write new string in the end of the line in csv file by C -
i writing c code need add new information in end of line if condition match.
file *file = fopen(filename, "r+"); //read every line if line!=null while (fgets(line, line_size, file)!= null){ //split line sep split(line, ',', fields); fprintf(file,",%d,%d,%d",d,f,g); }
it code try write matched information. however, write information in beginning of line. possible can move "file" pointer end of line?
probably need follow couple more steps
open temporary file along existing file
file *file = fopen(filename, "r"); file *tmpfile = fopen(tempfilename, "a+" );
get line existing file, trim next line append fields after comma, create comma separated string of fields
while (fgets(line, line_size, file)!= null){
remove new line , add paremeters
if( line[ strlen(line) - 1 ] == '\n' ) line[ strlen(line) - 1 ] = '\0'; fprintf(tmpfile,"%s,%s\n",line, your_field); // write temporary file }
delete old file unlink useful
man -a unlink
and rename temp file original file
man -a rename
Comments
Post a Comment