tags:

views:

163

answers:

4

Hi I am appending at the bottom of the text file this works on windows but on linux instead of appending to the bottom it appends to the top of the text file. The code i am using is. The code must only use C

FILE *fout;
fout = fopen("dat.txt","a");  
fprintf(fout,"&& ");
fclose(fout);

Please help. Thank you

+1  A: 

Check for errors returned from those system calls - that code looks correct. Also, check to make sure you aren't accidentally mangling dat.txt elsewhere in your code.

Chris
A: 

I compiled on Mac OS (Linux) and it works as advertised, e.g. the line was appended at the end.

Are you doing something else with the file?

stefanB
A: 

This should be ok. check your code again, maybe you are repostioning the file pointer, with frewind or fseek somewhere. A better way is to use the "a+" option. You should also check the return value fout from fopen.

bill
fseek and friends should not affect the write position of a file opened in append mode. Writes to append mode files always go to the end of file. If they don't, then the underlying C library is buggy.
laalto
A: 

If you do anything else with that file before the append, make sure you are closing it.

SP