简体   繁体   中英

File write in C confusion

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

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.

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

Are you doing something else with the file?

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.

如果在追加之前对该文件进行了其他操作,请确保将其关闭。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM