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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -