gpt4 book ai didi

c - 为什么我的程序在尝试将值存储在并行数组中时以 "Process exited with value 3221225477"终止?

转载 作者:行者123 更新时间:2023-11-30 19:02:31 25 4
gpt4 key购买 nike

我正在为模拟飞行系统的作业编写一个 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 人...

最佳答案

因此,除了每个人都给您提出一个简短示例的正确建议之外,我还花时间深入研究您的代码并发现您的第一个问题。

strcpy(*flightCityOrigin[i], token);

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

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

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

这也适用于您的后续数组。

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

关于c - 为什么我的程序在尝试将值存储在并行数组中时以 "Process exited with value 3221225477"终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55769774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com