简体   繁体   English

C编程,fprintf()无法正常工作

[英]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. 我从一个函数获取时间戳,但是该文件仅写时间戳,而不写标志。如果删除时间戳,则打印FLAG 1。 But Not getting together. 但是不在一起。 ie

Flag 1, Timestamp : 20141005141116

The output I am getting in test.txt like 我在test.txt中得到的输出像

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 请解决这个问题

You have the arguments to fprintf() in the wrong order. 您以错误的顺序输入了fprintf()的参数。 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: 假设time_stamp()返回一个静态字符串,则您的代码应为:

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

fprintf()参数应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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