Giga Tocka
Giga Tocka

Reputation: 191

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!

Upvotes: 1

Views: 46

Answers (2)

Giga Tocka
Giga Tocka

Reputation: 191

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

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 476990

Say this:

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

Upvotes: 3

Related Questions