ninja.stop
ninja.stop

Reputation: 430

C programming, fprintf() not working correctly

Probably a silly mistake I am doing here.

    FILE *fp;
    fp = fopen("test.txt", "a+");
    fprintf(fp, time_stamp(),"FLAG 1, Timestamp : %s\n");
    fclose(fp);

I am getting timestamp from a function But The file writing only timestamp, not the flag If I remove timestamp, FLAG 1 printing. But Not getting together. ie

Flag 1, Timestamp : 20141005141116

The output I am getting in test.txt like

20141005145640201410051456402014100514564020141005145640201410051456412014100514564120141

Not going to new line and print like:

Flag 1, Timestamp : 20141005141116
Flag 1, Timestamp : 20141005141117
Flag 1, Timestamp : 20141005141118

..... like that

Please solve this issue

Upvotes: 0

Views: 820

Answers (2)

unwind
unwind

Reputation: 399723

You have the arguments to fprintf() in the wrong order. Look at the manual page's prototype:

int fprintf(FILE *stream, const char *format, ...);

Clearly, the formatting string comes before the things being formatted (the variable part ...).

Assuming time_stamp() returns a static string, your code should be:

fprintf(fp, "FLAG 1, Timestamp : %s\n", time_stamp());

Upvotes: 8

Shwetha
Shwetha

Reputation: 150

Arguments to fprintf() should be like this :

fprintf(fp, "FLAG 1, Timestamp : %s\n", time_stamp());

Upvotes: 3

Related Questions