0

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

3
  • Please compile with all warnings & debug info (gcc -Wall -Wextra -g). You would get a warning from the compiler, if using GCC... Commented Nov 5, 2014 at 9:42
  • where is printf used? Commented Nov 5, 2014 at 9:42
  • @Zaibis: OP probably think of fprintf Commented Nov 5, 2014 at 9:43

2 Answers 2

8

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());
Sign up to request clarification or add additional context in comments.

5 Comments

fprintf("FLAG 1, Timestamp : %s\n",fp,timestamp()); Is that? actually doing c after a long time hence...basic problems
@anupam_on Uh no? Please look at the prototype, or the code I gave you in the answer above. First the file pointer, then the formatting string, then the things being formatted.
+1. This is possibly the nicest answer I have ever seen. Concise, to the point and all the relevant links.
@Joshpbarron Gee, thanks! I try, and in this regard I think practice really helps. :)
Joshpbarron ya, correctly said, very rare type of answering. But yes, some people are there to do things precisely, to the point and correctly :) Thanks buddy @unwind Problem solved :)
3

Arguments to fprintf() should be like this :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.