我已经花了整整一天的时间来试图弄清楚这段代码有什么问题,printf总是在屏幕上打印0。
#include <stdio.h>
#include <ctype.h>
int main()
{
int one=0,two=0;
FILE *arq;
arq = fopen ("testando.txt","w+");
fprintf(arq,"1,2,3\n");
fscanf(arq,"%d%d",&one,&two);
printf("%d %d\n",one,two);
return 0;
}
答案 0 :(得分:2)
fscanf(arq,"%d%d",&one,&two);
中添加逗号r
标志重新打开文件-进行读取这很好:
#include <stdio.h>
#include <ctype.h>
int main()
{
int one=0,two=0;
FILE *arq;
arq = fopen ("testando.txt","w+");
fprintf(arq,"1,2,3\n");
fclose(arq);
arq = fopen("testando.txt","r");
int r = fscanf(arq,"%d,%d",&one,&two);
fclose(arq);
printf("%d %d %d\n",r, one,two);
return 0;
}
输出:
2 1 2