C 缓冲区输出缺少名字

C 缓冲区输出缺少名字,c,file,memory-management,structure,C,File,Memory Management,Structure,嗨,我有一个问题,就是把数据读入结构数组 这就是我到目前为止所做的: #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME 20 #define FILE_NAME 50 #define LIST_SIZE 50 //void getData(RECORD name[], RECORD score) typedef struct { char

嗨,我有一个问题,就是把数据读入结构数组 这就是我到目前为止所做的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME   20
#define FILE_NAME  50
#define LIST_SIZE 50
//void getData(RECORD name[], RECORD score)


typedef struct 
{
    char *name;
    int  score;
}RECORD;


int main (void)
{
    // Declarations
       FILE *fp;
       char fileName[FILE_NAME];
       RECORD list[LIST_SIZE];
       char buffer[50];
       int count = 0;
    // Statements
       printf("Enter the file name: ");
       gets(fileName);

       fp = fopen(fileName, "r");

       if(fp == NULL)
           printf("Error cannot open the file!\n");
       while (fgets(buffer, LIST_SIZE, fp) != NULL)
         {   
                     printf("%s\n", buffer);
         list[count].name = (char*) calloc(strlen(buffer), sizeof(char));
         sscanf(buffer,"%s%d", list[count].name, &list[count].score);
         printf("name is: %s and score is:%d \n", list[count].name, list[count].score);
         count++;

         }
       printf("Read in %d data records\n", count);
       return 0;
}
而不是正确的输出

Enter the file name: in.txt
Ada Lovelace, 66

Linus Torvalds, 75

Peter Norton, 82

Ken Thompson, 82

Steve Wozniak, 79

Marc Andreessen, 60

Donald Knuth, 60

Adele Goldberg, 71

Grace Hopper, 82

Bill Joy, 91

Andrew Tanenbaum, 71

Brian Kernighan, 72
Read in 12 data records
这是我注释掉fscanf行后的输出,所以我认为错误可能发生在那里。内存分配过程是否有问题,我认为我做得不对

我现在修复了fscanf语句

Enter the file name: in.txt
Ada Lovelace, 66

name is: Ada and score is:3929744
Linus Torvalds, 75

name is: Linus and score is:3929752
Peter Norton, 82

name is: Peter and score is:3929760
Ken Thompson, 82

name is: Ken and score is:3929768
Steve Wozniak, 79

name is: Steve and score is:3929776
Marc Andreessen, 60

name is: Marc and score is:3929784
Donald Knuth, 60

name is: Donald and score is:3929792
Adele Goldberg, 71

name is: Adele and score is:3929800
Grace Hopper, 82

name is: Grace and score is:3929808
Bill Joy, 91

name is: Bill and score is:3929816
Andrew Tanenbaum, 71

name is: Andrew and score is:3929824
Brian Kernighan, 72
name is: Brian and score is:3929832
Read in 12 data records

正如您所看到的,行名是:只打印出名字,而不打印姓氏和整数分数的地址。

fgets和fscanf正在相互争斗,因为他们都在读取文件。使用fgets将文件读入缓冲区,然后使用sscanf将数据解析到结构中


一般来说,您应该更喜欢FGET而不是fscanf,因为它更安全。在任何情况下,都不要混合使用fgets和fscanf。

您的代码有几个问题。首先,如果输入文件中存在某种模式,请尝试使用scanf和fscanf。我在使用fgets和get时遇到了换行符问题。fscanf也会一直读取,直到找到空格字符或换行符为止(您在代码中考虑过这一点吗?因为很明显,为什么它只读取一个名称段)。可能是您使用的printf存在相同的问题,它只写入到\n或“”。 如果你可以使用C++,你可能想考虑使用IFStand和SsFrase. 试试这个:

...

while (fgets(buffer, LIST_SIZE, fp) != NULL)
{
    char *tok;
    printf("%s\n", buffer);

    tok = strchr(buffer, ',');
    if (!tok)
    {
        printf("Bad line\n");
        continue;
    }

    list[count].name = strndup(buffer, tok - buffer);
    list[count].score = atoi(tok + 1);

    printf("name is: %s and score is:%d \n", list[count].name, list[count].score);
    count++;
}
...

我确实解决了这个问题,而且当我在sscanf之后注释掉printf行时,它仍然只给我整数分数的名字和地址检查sscanf()的手册页。“%s”转换在空白处停止,除非您给它一个宽度说明符。
%s
是用于分隔符的空格字符。感谢它使它工作,但我仍然得到整数分数的地址打印输出。实际上,我编辑了printf行,现在分数
{char*name;float score;}RECORD
score
是float,如果是整数,则更改为int score。噢,是的,谢谢,我忘了更改回int
%s
转换在first和namite之间的空格处停止。然后,
%d
转换姓氏失败,您的
分数包含垃圾。a) 始终检查
(f/s)scanf的返回值
;2) 如果
score
int
,则使用
%[^,],%d
,或者如果
score
浮点
,则使用
%f
代替上面BLUEPIXY所说的
%d
...

while (fgets(buffer, LIST_SIZE, fp) != NULL)
{
    char *tok;
    printf("%s\n", buffer);

    tok = strchr(buffer, ',');
    if (!tok)
    {
        printf("Bad line\n");
        continue;
    }

    list[count].name = strndup(buffer, tok - buffer);
    list[count].score = atoi(tok + 1);

    printf("name is: %s and score is:%d \n", list[count].name, list[count].score);
    count++;
}
...