简体   繁体   中英

Appending a string on different lines of a file?

I am learning C and I had a question. I am trying to append a string into a file. However, every time a string is appended it has to be on the next line (Sort of like println instead of print).

I cannot make the function append on the next line. Instead, it just keeps appending on the same line. How do I do this?

void FileWriter(char *cmmd)
 {
    FILE *fp;
    fp = fopen("xxx.txt", "a");

    fprintf(fp, "%s", cmmd);
    fclose(fp);
 }

Thanks!

Say this:

fprintf(fp, "%s\n", cmmd);
//             ^^

I'm sorry I'm dumb. I put a \\n after %s and it worked. Maybe there is a better way?

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