简体   繁体   中英

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"

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. 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() .

Also, your format string is wrong, you cannot use * and hope it will skip some characters. Use BLUEPIXY's format. Or, parse the string using 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; 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; better make them 1 byte longer and adjust the last parameter in copywithquotes accordingly . The copywithquotes() function copies the input strings to the structure, ignoring the quotes that some of your strings have at their beginning and end. (Maybe i should have named it copy* without *quotes, but i was thinking of copying a string that had quotes to something usable).

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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