简体   繁体   English

我的程序没有将输入数据存储到C中的结构中

[英]My program isn't storing the input data into the struct in C

I am currently working on an assignment that takes in a file with student data and outputs the data reformatted into something easy to read. 我目前正在做一个作业,该作业需要一个包含学生数据的文件,然后将数据重新格式化为易于阅读的格式。

So how it works right now is that is should open the file, read the data into the structure's different parts, and then print out the data in a tabular format. 因此,现在的工作方式是打开文件,将数据读入结构的不同部分,然后以表格格式打印出数据。 But I kept running into problems. 但是我一直遇到问题。 I modified just to test what was happening and found that I would get something completely different --> it would print an empty variable. 我修改只是为了测试正在发生的事情,发现我会得到完全不同的东西->它会打印一个空变量。 What am I doing wrong with how I'm scanning in data? 我扫描数据的方式有什么问题?

Input from file 1205,970546325,"Chen","Xiwei",ESE, 230,="R01" 来自文件1205,970546325,“ Chen”,“ Xiwei”,ESE,230,=“ R01”的输入

Output Weird characters that I can't even copy 输出我什至无法复制的怪异字符

struct student {
    long int term;
    long long int sID;
    char lastName[15];
    char firstName[15];
    char subject[3];
    int catalog;
    char section[3];
};

int main(void){
    struct student list[10];
    char fileName[50];
    FILE *inputFile;

    printf("Please enter the name of the file: ");  //get file name
    scanf("%s", &fileName);
    inputFile = fopen(fileName, "r");               //open filelist[i].

    if (inputFile == NULL)
        perror("ERROR");

    int i = 0;
    char test1[150];    

    while (fgets(test1, 150, inputFile) != NULL){       //as long as it's not the EOF, keep reading in the following format
        fscanf(inputFile, "%li*%ll**%^[,]***%^[,]**%^[,]*%i***%s*", 
        &list[i].term, &list[i].sID, &list[i].lastName, &list[i].firstName,
        &list[i].subject, &list[i].catalog, &list[i].section);

        i++;
    }

    printf("Last Name, First Name\t\t\tTerm\tID\t\tCourse\tSection\n");

    printf("%s, %s", list[0].lastName, list[0].firstName);

    getchar();
    getchar();
    return 0;
}

You want to use sscanf() on the test1 variable, not fscanf() from your inputfile. 您想在test1变量上使用sscanf() ,而不要在输入文件中使用fscanf() Especially not without checking for EOF and how many variables get actually read after you've read the only line the file has with fgets() . 特别是在没有检查EOF以及在fgets()读取文件中仅有的一行之后实际读取多少个变量的情况下。

Also, your format string is wrong, you cannot use * and hope it will skip some characters. 另外,格式字符串错误,不能使用*并希望它会跳过一些字符。 Use BLUEPIXY's format. 使用BLUEPIXY的格式。 Or, parse the string using strtok: 或者,使用strtok解析字符串:

char *p;
while (fgets(test1, 150, inputfile)!=NULL) {
    if ((p=strtok(test1, ","))==NULL) continue;
    list[i].term=atol(p);
    if ((p=strtok(NULL, ","))==NULL) continue;
    list[i].sID=atol(p);
    if ((p=strtok(NULL, ","))==NULL) continue;
    copywithquotes(list[i].lastName, p, 15);
    if ((p=strtok(NULL, ","))==NULL) continue;
    copywithquotes(list[i].firstName, p, 15);
    if ((p=strtok(NULL, ","))==NULL) continue;
    copywithquotes(list[i].subject, p, 3);
    if ((p=strtok(NULL, ","))==NULL) continue;
    list[i].catalog=atoi(p);
    if ((p=strtok(NULL, "\n"))==NULL) continue;
    if (*p=='=')
        p++;
    copywithquotes(list[i].section, p, 3);
    i++;
}

/* copy string from to to. Stop after maxsize bytes. Ignore quotes in the original string */

void copywithquotes(char *to, char *from, int maxsize) {
    int copied=0;
    while (*from && copied<maxsize) {
        while (*from=='"')
            from++;
        *to++=*from++;
        copied++;
    }
    if (copied<maxsize)
        *copied='\0';
}

The various if (strtok...==NULL) continue lines make the loop ignore non-wellformed input; 各种if (strtok...==NULL) continue行使循环忽略非格式正确的输入; in a real program, they should probably emit some warning. 在实际程序中,它们可能会发出一些警告。 Also, note that your char[] structure members don't provide space for the '\\0' string end markers; 另外,请注意,您的char []结构成员不为'\\ 0'字符串结束标记提供空间; better make them 1 byte longer and adjust the last parameter in copywithquotes accordingly . 最好将它们增加1个字节,并相应地调整copywithquotes中的最后一个参数。 The copywithquotes() function copies the input strings to the structure, ignoring the quotes that some of your strings have at their beginning and end. copywithquotes()函数将输入字符串复制到结构中,而忽略了某些字符串在开头和结尾都有的引号。 (Maybe i should have named it copy* without *quotes, but i was thinking of copying a string that had quotes to something usable). (也许我应该将其命名为copy * 而不带 *引号,但我正在考虑将带有引号的字符串复制为可用的东西)。

try this 尝试这个

while (fgets(test1, 150, inputFile) != NULL){
   sscanf(test1, "%ld,%lld,\"%[^\"]\",\"%[^\"]\",%3c,%i,=\"%3c\"", 
    &list[i].term, &list[i].sID, list[i].lastName, list[i].firstName,
    list[i].subject, &list[i].catalog, &list[i].section);

    i++;
}

side note 边注

scanf("%49[^\n]", fileName);
inputFile = fopen(fileName, "r");

if (inputFile == NULL){
    perror("ERROR of fopen");
    return -1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM