user-0 提问: 求助！读取文件中数据然后输出时每次循环都会多一个0，代码如下，怎么办？

回复:
user: user-1
content: 在你的程序加一个判断也可以 #include <stdio.h> #include <stdlib.h> #include <string.h>  int main(void) {    char str[90] = { '\0' };    double a[11];    char *b = NULL;    int i;    FILE *fp1 = fopen("3.txt", "r");    if (fp1)    {         do       {          if (!fgets(str, 89, fp1)) break; //           b = strtok(str, " ");          for (i = 0; i < 11; ++i)          {              if (b)             {                a[i] = atof(b);                b = strtok(NULL, " ");                printf("%f ", a[i]);              }          }                                                                                               printf("\n");       } while (!feof(fp1));       fclose(fp1);    }    else    {       printf("无文件\n");       return -1;    }    return 0; }

user: user-2
content: 如果文件是固定格式的，可以使用 fscanf() 函数读数据 例如文件内容为 str 1.0 20.0 10.12 可以这样写 fscanf(fp, "%s %f %f %f", ...);

user: user-1
content: 直接用fgets判断文件尾 #include <stdio.h> #include <stdlib.h> #include <string.h>  int main(void) {    char str[90] = { '\0' };    double a[11];    char *b = NULL;    int i;     FILE *fp1 = fopen("3.txt", "r");    if (fp1)    {         while (fgets(str, 89, fp1))       {            b = strtok(str, " ");          for (i = 0; i < 11; ++i)          {              if (b)             {                a[i] = atof(b);                b = strtok(NULL, " ");                printf("%f ", a[i]);              }          }                                                                                               printf("\n");       }       fclose(fp1);    }    else    {       printf("无文件\n");       return -1;    }    return 0; }

user: user-1
content: 或者用 fgetc 预读一个字符再判断文件尾 #include <stdio.h> #include <stdlib.h> #include <string.h>  int main(void) {    char str[90] = { '\0' };    double a[11];    char *b = NULL;    int i;    int c;     FILE *fp1 = fopen("3.txt", "r");    if (fp1)    {         do       {          c = fgetc(fp1);          if (c == EOF) return -2;          else ungetc(c, fp1);                fgets(str, 89, fp1);                    b = strtok(str, " ");          for (i = 0; i < 11; ++i)          {              if (b)             {                a[i] = atof(b);                b = strtok(NULL, " ");                printf("%f ", a[i]);              }          }                                                                                               printf("\n");       } while (!feof(fp1));       fclose(fp1);    }    else    {       printf("无文件\n");       return -1;    }    return 0; }

user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  int main(void) {    char str[90] = { '\0' };    double a[11];    char *b = NULL;    int i;    FILE *fp1 = fopen("3.txt", "r");    if (fp1)    {         //文件最后没有单独的换行        #if 01       do       {          fgets(str, 89, fp1);                    b = strtok(str, " ");          for (i = 0; i < 11; ++i)          {              if (b)             {                a[i] = atof(b);                b = strtok(NULL, " ");                printf("%f ", a[i]);              }          }                                                                                               printf("\n");       } while (!feof(fp1));       fclose(fp1);       #endif       //文件最后有单独的换行       #if 0       fgets(str, 89, fp1);       while (!feof(fp1))        {          b = strtok(str, " ");          for (i = 0; i < 11; ++i)          {              if (b)             {                a[i] = atof(b);                b = strtok(NULL, " ");                printf("%f ", a[i]);              }          }                                                                                               printf("\n");          fgets(str, 89, fp1);       }       #endif     }    else    {       printf("无文件\n");       return -1;    }    return 0; }

user: user-3
content: main函数没有定义位double类型的。  建议把你的文件内容贴出来，另外，顺便把str打出来，看看strtok之前str是什么字符串； 另外注意fgets会将'\n'换行符读到str里。

user: user-4
content: 是不是每一行最后一列数据后面由空格啊，你可以用while判断strtok结束，并结合判断a[i]的值是否为0.0来控制输出。