Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么fscanf只从文件中读取一个字符串的第一个字_C_File_Scanf - Fatal编程技术网

为什么fscanf只从文件中读取一个字符串的第一个字

为什么fscanf只从文件中读取一个字符串的第一个字,c,file,scanf,C,File,Scanf,当我试图用fscanf()读取结构的文件信息时,我遇到了一个问题。它只读取字符串的第一行,循环永远不会结束。我怎样才能解决这个问题 结构 typedef struct { int id; char name[80]; char nameStadium[80]; int numberPlacesStadium; float funds; float monthlyExpenses; int active; } Team; 我用这个代码来读

当我试图用
fscanf()
读取结构的文件信息时,我遇到了一个问题。它只读取字符串的第一行,循环永远不会结束。我怎样才能解决这个问题

结构

typedef struct {
    int id;
    char name[80];
    char nameStadium[80];
    int numberPlacesStadium;
    float funds;
    float monthlyExpenses;
    int active;
} Team;
我用这个代码来读

void showAll(void)
{
    FILE* file;
    Team team;

    file = fopen("file.txt", "rt");

    if (file == NULL)
    {
        printf("!!!Cant open file!!!\n");
        return;
    }

    rewind(file);

    printf("\n\n=== TEAMS ======\n");
    printf("%s\t%s\n", "ID", "NAME");

    while (fscanf(file, "%6d %s %s %6d %f %f %03d\n", &team.id, team.name, team.nameStadium, &team.numberPlacesStadium, &team.funds, &team.monthlyExpenses, &team.active) != EOF)
    {
        if (team.active != 0)
        {
            printf("%d\t%s\n", team.id, team.name);
        }
    }

    fclose(file);

}
我不明白为什么
fscanf()
只获取第一个单词而不是完整的字符串


有人知道如何解决这个问题吗?

我刚刚用一个按照您发布的格式制作的示例文本文件测试了您的代码。所有内容都已读入,除了没有正确关闭文件外,似乎一切正常。应该是这样的

fclose(file);
如果希望代码能够读取带空格的字符串,最好使用分隔符系统。下面的代码读取它们的类型(假设整数之间没有空格),读取80个非逗号字符的序列(这将是名称),然后用逗号分隔其他数字

  while (fscanf(file, "%d, %80[^,], %80[^,], %d, %f, %f, %d\n", &team.id, 
     team.name, team.nameStadium, &team.numberPlacesStadium, &team.funds, 
    &team.monthlyExpenses, &team.active) != EOF) 
  {
      if (team.active != 0)
    {
        printf("%d\t%s %s\n", team.id, team.name, team.nameStadium);
    }
  }
我已经对它进行了测试,它确实有效,但请记住,这也不是最优雅的解决方案

以下是我的文本文件中的行的外观:

 133222, Bears, Bears Stadium, 444333, 23, 35.3, 52
 666222, Eagles, Eagles Stadium, 322222, 13, 56.3, 54

1) 不要与
EOF
进行比较,与预期成功进行比较,而(fscanf(文件,“%6d%s%s%6d%f%f%f%03d\n”,…==7)post sample file.txt。更好的是,3)
%03d
是有问题的。试试
%3d
。4) 名称/视距是否包含空格?您的问题标题说明“fscanf仅读取第一个单词”,但后面的文本中说“它仅读取第一行”,那么fscanf是否在第一个单词或第一行之后停止工作?如果在尝试将
team.name
作为字符串(
%s
)读取时,它在第一个单词后停止,则此帖子()可能会有所帮助。无论如何,正如@chux-restore Monica已经评论过的那样,示例输入是有用的。
char*path=“file.txt”;如果((file=fopen(path,“r”))==NULL){perror(path);…}
。错误消息很重要。它们应该告诉您失败的原因,并写入正确的流。请参阅,请注意,
%s
扫描格式在第一个空格处停止,而在打印格式中,它会打印整个字符串-空格、换行符和所有字符。因此,可以使用生成数据时使用的格式打印无法读回的数据。你怎么知道哪个词属于体育场名称?您能否使用扫描集(如
%79[^0-9]
)将输入控制到第一位(假设体育场名称不包含数字,这可能不安全)。人们使用分隔符(除空格外)有很多原因——这就是一个例子。您的字符串是否有多个单词??关于fclose错误是文章中的一个输入错误,这个问题可能与我使用Visual Studio IDE有关吗?是的,但是字符串没有空格,它们有一个特殊的字符,如果你有空格,它将无法正常工作。我的意思是,从技术上讲,你可以,但是它看起来非常松散,在扫描时很难跟踪。大多数带有文本文件的文件I/O都是使用某种分隔符完成的,以方便和稳定,该分隔符通常是逗号。它是Access和Excel等数据库的标准。