Take the 2-minute tour ×

I want to print some values into the text file which contains some data. The problem is that I want to print data at the end of each line after the data that is already in text file.

Here is my text file:

100 1 
120 2
160 6
200 7
300 6


    fseek(txt, 2, SEEK_SET);
    fprintf(txt," %d\n",num[m]);
share|improve this question

2 Answers 2

up vote 4 down vote accepted

The only way to do that in C:

  1. read in every line
  2. append the data you want to the end of the line, or just rewrite the whole line
  3. and write the line to a new file.

You could do that using sed/awk.

share|improve this answer
@alk Sorry, I mean a new file –  gongzhitaao Mar 31 '13 at 18:33

use another file..copy each line of original file alternatively with your "new" file...after you are done remove the original file and rename it to the original file's name

while(fread()) { copy to temp file. & add the new line to the temp file }

remove(original); rename(temp,original);

share|improve this answer
i mean copy 1 line..and add the desired line..till you are done. –  Darshan Shah Mar 31 '13 at 18:43

Your Answer


