在尝试将值存储在并行数组中时,为什么我的程序以“进程退出值为3221225477”结束? [重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我正在为模拟飞行系统的任务编写C程序。文件读取函数必须从文件中填充并行数组,因此我选择逐行读取fgets。 read_flight函数对每一行进行标记,并将标记保存到通过引用传递的数组中(文件中最多30行)。每次保存后的printf语句只是用于验证每个变量是否已正确保存的临时代码。这段代码适用于前几行,然后程序终止,返回值为3221225477.从我在本网站上看到的内容来看,该代码表示​​分段错误,但我不能为我的生活找出原因,因为它与我读过的其他人不同。

如果我减少传递给read_flight函数的数组的第二维的大小,代码在终止之前由于某种原因运行得更远。

我的代码是(终止发生在read_flight函数中):

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

//Function signatures
int menu (void);
void modify_flight(void);
void modify_reservation(void);
void report(void);
void read_flight(int (*flightNumber)[30],
        char (*flightCityOrigin)[30][15],
        char (*flightCityDest)[30][15],
        char (*flightDate)[30][15],
        char (*flightTime)[30][10],
        int (*flightSeats)[30]);
void read_reservation(int (*resCode)[30],
        int (*flightNum)[30],
        char (*lName)[30][15],
        char (*fName)[30][15],
        char (*seatType)[30][8],
        double (*seatCost)[30]);
void exit_program(void);
void clear (void);

//Global variables
const char FLIGHT_FILE[] = "flightc.txt";   //File name of flights file
const char RES_FILE[] = "reservationc.txt"; //File name of reservation file

int main(int argc, char *argv[]) {

    int menuSel;    //Integer to store menu selection

    //Define parallel arrays to store information from flight file
    int flightNumber[30];
    char flightCityOrigin[30][15];
    char flightCityDest[30][15];
    char flightDate[30][15];
    char flightTime[30][10];
    int flightSeats[30];

    //Define parallel arrays to store info from reservation file
    int resCode[30];
    int flightNum[30];
    char lName[30][15];
    char fName[30][15];
    char seatType[30][8];
    double seatCost[30];

    //Call init functions
    read_flight(&flightNumber, &flightCityOrigin, &flightCityDest, &flightDate, &flightTime, &flightSeats);
    read_reservation(&resCode, &flightNum, &lName, &fName, &seatType, &seatCost);

    //Display menu in a loop until user chooses option 4 (exit system)
    do {
        //Call our Menu function to get our selection value
        menuSel = menu();

        //Call appropriate function as per menu selection
        switch (menuSel) {
            case 1:
                modify_flight();
                break;
            case 2:
                modify_reservation();
                break;
            case 3:
                report();
                break;  
        }

    } while (menuSel != 4);

    //Call exit program function
    exit_program();

    //Return with a normal exit code of 0
    return 0;
}

//This function prints the menu and returns the user's selection as an integer by value
int menu (void) {

    //Print menu
    printf("\n\tACME Airline System\n\n");
    printf("\n1.\tAdd/modify flight information");
    printf("\n2.\tAdd/modify reservation information");
    printf("\n3.\tReport section");
    printf("\n4.\tExit airline system\n\n");

    //Perform input in a do-while-loop for input validation
    int choice;

    do {
        printf("\n\tPlease make your selection >");
        scanf("%i", &choice);
        clear();
    } while (choice > 4 || choice < 1);

    return(choice);
}

//Stub
void modify_flight(void) {
    printf("\n(Inside modify flight function)\n");
}

//Stub
void modify_reservation(void) {
    printf("\n(Inside modify reservation function)\n");
}

//Stub
void report(void) {
    printf("\n(Inside report function)\n");
}

//Read flight file and parse lines into variables to fill the arrays passed by pointer
void read_flight(int (*flightNumber)[30],
        char (*flightCityOrigin)[30][15],
        char (*flightCityDest)[30][15],
        char (*flightDate)[30][15],
        char (*flightTime)[30][10],
        int (*flightSeats)[30]) {

    //Open flight file for reading
    FILE *flightFile;
    flightFile = fopen(FLIGHT_FILE, "r");

    if (flightFile) {

        char *token;    //Stores the current string token
        int i = 0;      //Counter variable for parallel arrays
        char line[60];  //Stores each line read with fgets()

        //Read file line by line, and parse each value into our parallel arrays. This
        //function assumes that the flight file is in the correct format for tokenizing,
        //I.E., that it contains the same number and format of tokens in each line.
        while (fgets(line, 60, flightFile)) {

            //Print line for testing
            printf("%s", line);


            //Get first token and store as flight number
            token = strtok(line, " ");
            *flightNumber[i] = atoi(token);

            //Test
            printf("\n%d", *flightNumber[i]);

            //Store second token as origin city
            token = strtok(NULL, " ");
            strcpy(*flightCityOrigin[i], token);

            //Test
            printf("\n%s", *flightCityOrigin[i]);

            //Store third token as destination city
            token = strtok(NULL, " ");
            strcpy(*flightCityDest[i], token);

            //Test
            printf("\n%s", *flightCityDest[i]);

            //Store fourth token as flight date
            token = strtok(NULL, " ");
            strcpy(*flightDate[i], token);

            //Test
            printf("\n%s", *flightDate[i]);

            //Store fifth token as flight time
            token = strtok(NULL, " ");
            strcpy(*flightTime[i], token);

            //Test
            printf("\n%s", *flightTime[i]);

            //Get sixth token and store as number of seats available
            token = strtok(NULL, " ");
            *flightSeats[i] = atoi(token);

            //Test
            printf("\n%d\n\n", *flightSeats[i]);

            //Increment counter for next line
            i++;
        }

        //Free resources
        fclose(flightFile);
    }
}

//Read reservation file and parse lines into variables to fill the arrays passed by pointer
void read_reservation(int (*resCode)[30],
        int (*flightNum)[30],
        char (*lName)[30][15],
        char (*fName)[30][15],
        char (*seatType)[30][8],
        double (*seatCost)[30]) {

    printf("\n(Inside read reservation function)\n");

}

//Stub
void exit_program(void) {
    printf("\n(Inside exit program function)\n");
}

//This function clears the input buffer after a scanf call to prevent input errors
void clear (void) {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) {}
}

输入文件flightc.txt如下:

1234 Pittsburgh Phoenix 04/02/19 10:30am 108
4567 Boston New_York 04/03/19 18:10am 210
9876 Pittsburgh Seattle 04/04/19 1:30pm 110
8888 Chicago Pittsburgh 04/05/19 2:45pm 106
7777 Pittsburgh Miami 04/06/19 9:35am 116
2892 Las_Vegas New_York 04/07/19 8:10pm 107
4444 Pittsburgh San_Francisco 04/19/18 5:55pm 124
2222 Atlanta New_York 04/09/19 4:30pm 110
9281 Pittsburgh Seattle 04/11/19 2:15pm 216
1000 Pittsburgh Phoenix 04/12/19 3:25pm 150

从理论上讲,它应该填充这些数组,然后在到达文件末尾时退出,但它会运行几行然后终止。输出如下所示:

1234 Pittsburgh Phoenix 04/02/19 10:30am 108

1234
Pittsburgh
Phoenix
04/02/19
10:30am
108

4567 Boston New_York 04/03/19 18:10am 210

4567
Boston
New_York
04/03/19
18:10am
210

9876 Pittsburgh Seattle 04/04/19 1:30pm 110

9876
Pittsburgh
Seattle
04/04/19
1:30pm
110

8888 Chicago Pittsburgh 04/05/19 2:45pm 106

8888
--------------------------------
Process exited after 0.8375 seconds with return value 3221225477
Press any key to continue . . .

非常感谢你们所有帮助,你们可以帮助我找出它为什么突然停止按计划运作。我是一个Java家伙,而不是C家伙......

c pointers multidimensional-array parameter-passing fgets
1个回答
1
投票

因此除了每个人给你提供一个简短例子的正确建议外,我还花时间深入挖掘你的代码并发现你的第一个问题。

strcpy(*flightCityOrigin[i], token);

[]优先于*,因此索引计算首先发生在*间接之前,这不是你的意图。我可以在调试窗口中看到,只有第一个令牌被复制到flightCityOrigin的第0个索引中。

解决方案是指定正确的运算符优先级,如下所示

strcpy((*flightCityOrigin)[i], token);

这也适用于您的后续阵列。

如果你仍然无法弄清楚其余部分,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.