请帮帮我! enter image description here
我的文字文件在这里。我想读所有这些,但我可以拿一半。我不知道为什么..
你能查一下我的代码吗? thnx很多。
FILE *fp;
char temp[10][250];
int i,j;
if((fp=fopen("init.txt","r"))==NULL)
{
printf("Reading Error!!");
}
fscanf(fp,"%d\n%d,%d", &botanist->water_bootle_size, &forest->height, &forest->width);
printf("%d %d %d ",botanist->water_bootle_size, forest->height, forest->width);
for(i=0;i<forest->height;i++)
{
fgets(temp[i],forest->width*2+1,fp);
}
for(int j=0;j<10;j++)
{
printf("%s",temp[j]);
}
fclose(fp);
答案 0 :(得分:2)
您的测试是错误的。它应该是:
if ((fp = fopen("init.csv", "r")) == NULL)
第二个电话fp = fopen("init.cvs","r");
是多余的,应该删除。
fscanf()
也不是解析CSV文件的好工具:
,
的行为可能不正确。假设文件内容简单,您应该分别解析第一行以处理列号并以这种方式更改fscanf()
:
if (fscanf(fp,"%d,%d,%d,%49[^,],%49[^,],%49[^,],%49[^,],%49[^,],%49[^,],%49[^,],%49[^,],%49[^,],%49[^,]",
&water, &x, &y, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) == 13) {
/* 13 fields correctly parsed */
}
答案 1 :(得分:0)
你的fopen行应该阅读(看看区别:首先分配,然后检查结果)。
if (NULL == (fp = fopen("init.csv","r"))){
答案 2 :(得分:0)
代码块
if((fp = fopen("init.csv","r")==NULL)) {
}
首先fopen("init.csv","r")==NULL)
评估错误&amp;如果文件存在,则给出0
(比较运算符)&amp;然后fp = 0
评估哪个不希望你的意图。
if( (fp = fopen("init.csv","r")) == NULL) {
fprintf(stderr," some messagee ");
return 0;
}
else {
printf("it works");
}
//fp = fopen("init.cvs","r"); /* why open again */
fscanf(); /* check the return value */