我写了一个程序来保存足球队的统计数据。我将统计信息保存在一个文件中,并将所有匹配记录在另一个名为“record.txt”的文件中。在写入文件时获取不同的输出
在我record.txt文件,书写格式为:
[对手名称] [当前日期]
我现在面临的问题是,有时我得到“对手的名字”在一行中‘日期’,有时日期在新线
这里开始就是我得到 pic of output file
的图像这里是我的代码
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"%d/%m/%y",timeinfo);
/* current date acquired */
FILE *history;
history = fopen("record.txt", "a");
char opponent[10]; //opponent name
printf("Opponent Name: ");
fgets(opponent, 10, stdin);
fprintf(history, "Opponent: %s %s\n", opponent, buffer);
fclose(history);
请在此重新发布代码和示例输出,不是每个人的网络都允许pastebin或imgur。 – IllusiveBrian 2015-02-09 17:35:28
你根本不执行任何检查,'fopen()'/'fgets()'可能会失败,你必须检查它。 – 2015-02-09 17:36:39
我已经发布了代码。不幸的是,我不允许张贴图片 – 2015-02-09 17:37:36