简体   繁体   English

在C中读取struct时fscanf崩溃

[英]fscanf crash while reading to struct in C

I am developing some C app for my homework and I'm facing with annoying crash. 我正在为自己的作业开发一些C应用程序,但我面临着烦人的崩溃。 Here's my code: 这是我的代码:

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

//Constants
//Available user choices
enum commands {READ_LIST = 1, QUIT};

struct student {
    char* surname;
    char* name;
    char* group;
};

typedef struct student Student;
typedef Student * studentPtr;

//Globals
int studentCount = 0;

//Function declarations
void displayCommands();
void readList();

//Main function
int main() {

    char enteredValue[999];
    int thisCommand;
    int running = 1;

    while(running) {
        displayCommands();
        scanf("%s", enteredValue);
        thisCommand = atoi(enteredValue);
        puts("\n----------------------------------------------");
        switch(thisCommand) {
            case READ_LIST:
                readList();
                break;
            case QUIT:
                running = 0;
                break;
            default:
                puts("Wrong command!");
                break;
        }
    }
    system("pause");
    return 0;
}

void displayCommands() {
    puts("\n---------------------------------------------");
    puts("Enter a command number:");
    printf("%d - Read students from file.\n", READ_LIST);
    printf("%d - Quit.\n", QUIT);
    puts("----------------------------------------------");
}

void readList() {
    FILE *fp = NULL;

    fp = fopen("studs.txt", "r");

    studentPtr newStudentPtr = malloc(sizeof(Student));

    if(fp != NULL) {
        fscanf(fp, "%d", &studentCount);

        if(newStudentPtr != NULL) {
            fscanf(fp, "%s %s %s", newStudentPtr->surname,
                   newStudentPtr->name, newStudentPtr->group);
        }
        fclose(fp);
    } else {
        puts("Unable to open file for reading!");
    }
}

I basically try to read integer ant three strings into a structure. 我基本上尝试将整数ant三个字符串读入结构。 The program crashes when it's trying to read last line of the file. 尝试读取文件的最后一行时,程序崩溃。

Any help, please? 有什么帮助吗? What mistakes am I making? 我犯什么错误?

Thanks in advance! 提前致谢!

You cannot read into an arbitrary memory address. 您不能读入任意的存储器地址。 After you allocate memory to the student structure you also need to allocate memory for each string. 在为学生结构分配内存后,还需要为每个字符串分配内存。 Crudely: 残酷地:

    if(newStudentPtr != NULL) {
        char buffer[3][256];
        fscanf(fp, "%s %s %s", buffer[0], buffer[1], buffer[2]);
        newStudentPtr->surname = malloc(strlen(buffer[0])+1);
        strcpy(newStudentPtr->surname, buffer[0]);
        newStudentPtr->name = /* similar ... */;
        newStudentPtr->group = /* similar ... */;
    }

You can change your struct student to contain array of chars (in place of char pointers); 您可以将struct student更改为包含char数组(代替char指针);

struct student {
    char surname[100];
    char name[100];
    char group[100];
};

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

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