【发布时间】:2019-10-01 22:35:35
【问题描述】:
我有这个代码。假设我想将航班 1(使用 InputFlight 函数生成)链接到用户 1(使用 InputUser 函数生成)。 注意:结构 user 有另一个嵌套结构称为 booking,它有一个嵌套结构称为 飞行。
通过在二进制文件中移动指针,我所做的只是:
询问用户一个航班号(假设是第一个航班)-> 所以我移动到第一个航班。
询问用户他是哪个用户(假设是第一个用户)-> 所以我转向第一个用户。
然后通过 fread() 和 fwrite(),我尝试复制结构 flight 中的内容进入 travelers.b.list,但不知何故它不起作用。
我已经尝试了一切。我尝试更改 fwrite() 和 fseek() 中的参数,但没有成功。我猜主要的问题是在booking.h里面的checkin()函数,但是我好像解决不了。
main.c
#include <stdio.h>
#include <stdlib.h>
#include "flight.h"
#include "user.h"
void login_admin();
int main() {
setbuf(stdout, NULL);
login_admin();
return 0;
}
void login_admin() {
FILE *file;
FILE *file1;
unsigned short int userchoice1 = 0;
unsigned short int s = 0;
while (s != 1) {
printf("\n-------------- M E N U --------------");
printf("\n----- A D M I N I S T R A T O R -----");
printf("\n1) Insert user");
printf("\n2) Insert flight");
printf("\n3) Check-in");
printf("\n9) Exit program");
printf("\n-------------------------------------");
printf("\nYOUR CHOICE: ");
scanf("%hu", &userchoice1);
if (s != getchar()) {
}
switch (userchoice1) {
case 1:
inputUser(file);
break;
case 2:
inputFlight(file);
break;
case 3:
checkin(file, file1);
break;
case 9:
s = 1;
exit(0);
break;
default:
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\nValue not valid.");
break;
}
}
}
booking.h
#include <stdio.h>
typedef struct {
flight list;
char booking_code[MAX_BOOKING_CODE];
} booking; //struct booking
typedef struct {
char name[MAX_NAME];
char surname[MAX_SURNAME];
date date_of_birth;
char place_of_birth[MAX_PLACE_OF_BIRTH];
char passport_number[MAX_PASSPORT];
booking b;
} user; //struct user
void checkin(FILE *flights, FILE *users) {
flight list;
user travelers;
int user_number = 0;
int choice = 0;
int choice1= 0;
if( (users = fopen("users.dat", "rb") ) == NULL) {
printf("Error.(1)");
}
else
{
printf("\nWhat's your user number?");
printf("\nUSER NUMBER: ");
scanf("%d", &user_number);
fseek(users, (user_number-1)*sizeof(user), SEEK_SET);
fread(&travelers, 1, sizeof(user), users);
if( (flights = fopen("flights.dat", "rb") ) == NULL) {
printf("Error.");
}
else {
printf("Available flights:\n\n");
flights = fopen("flights.dat", "rb");
while( !(feof(flights)))
{
int read = fread(&list, 1, sizeof(flight), flights);
if ( read > 0) {
printf("Flight code: %s\nCompany name: %s\nDEPARTURE FROM: %s\nARRIVAL AT: %s\nPlane code: %s\nDay of flight: %hu\nMonth of flight: %hu\n"
"Year of flight: %hu\nHour of departure: %hu:%hu\nHour of arrival: %hu:%hu\nFlight time: %hu min.\n\n",
list.flight_code, list.companyname, list.departure,
list.arrival, list.plane_code, list.date_of_flight.day,
list.date_of_flight.month, list.date_of_flight.year, list.hour_departure,
list.minute_departure,list.hour_arrival,list.hour_departure, list.flight_time);
}
}
printf("Which flight you want to choose?\n");
printf("YOUR CHOICE: ");
scanf("%d", &choice1);
fseek(flights, (choice1-1)*sizeof(flight), SEEK_SET);
fread(&list, 1, sizeof(flight), flights);
printf("\n--------YOU CHOSE FLIGHT %d-------- \n\n", choice1);
printf("Flight code: %s\nCompany name: %s\nDEPARTURE FROM: %s\nARRIVAL AT: %s\nPlane code: %s\nDay of flight: %hu\nMonth of flight: %hu\n"
"Year of flight: %hu\nHour of departure: %hu:%hu\nHour of arrival: %hu:%hu\nFlight time: %hu min.\n\n",
list.flight_code, list.companyname, list.departure,
list.arrival, list.plane_code, list.date_of_flight.day,
list.date_of_flight.month, list.date_of_flight.year, list.hour_departure,
list.minute_departure,list.hour_arrival,list.hour_departure, list.flight_time);
users = fopen("users.dat", "rb+");
fseek(users, (user_number-1)*sizeof(user), SEEK_SET);
fwrite(&travelers.b.list, 1, sizeof(user), users);
fclose(users);
fclose(flights);
flight.h
#include <stdio.h>
#include "define.h"
typedef struct {
unsigned short int day;
unsigned short int month;
unsigned short int year;
} date; //struct date
typedef struct {
char flight_code[MAX_FLIGHT_CODE];
char companyname[MAX_COMPANY_NAME];
char departure[MAX_DEPARTURE];
char arrival[MAX_ARRIVAL];
char plane_code[MAX_PLANE_CODE];
unsigned short int seats[MAX_SEATS];
date date_of_flight;
unsigned short int hour_departure;
unsigned short int minute_departure;
unsigned short int hour_arrival;
unsigned short int minute_arrival;
unsigned short int flight_time;
} flight; //struct flight
void inputFlight(FILE *flight_file);
void inputFlight(FILE *flight_file) {
flight list;
static int i = 0;
//for (int i = 0; i < 20; i++) {
flight_file = fopen("flights.dat","wb");
printf("Flight code %d: ", i+1);
scanf("%6s", list.flight_code);
fflush(stdin);
printf("Company name of flight %d: ", i+1);
scanf("%19s", list.companyname);
fflush(stdin);
printf("DEPARTURE: ");
scanf("%19s", list.departure);
fflush(stdin);
printf("ARRIVAL: ");
scanf("%19s", list.arrival);
fflush(stdin);
printf("Plane code of flight %d: ", i+1);
scanf("%4s", list.plane_code);
fflush(stdin);
printf("Day of flight %d: ", i+1);
scanf("%hu", &list.date_of_flight.day);
fflush(stdin);
printf("Month of flight %d: ", i+1);
scanf("%hu", &list.date_of_flight.month);
fflush(stdin);
printf("Year of flight %d: ", i+1);
scanf("%hu", &list.date_of_flight.year);
fflush(stdin);
printf("Hour and minutes of departure flight %d (separated by spacebar): ", i+1);
scanf("%hu%hu", &list.hour_departure, &list.minute_departure);
fflush(stdin);
printf("Hour and minutes of arrival flight %d (separated by spacebar): ", i+1);
scanf("%hu%hu", &list.hour_arrival, &list.minute_arrival);
fflush(stdin);
printf("Flight time: ");
scanf("%hu", &list.flight_time);
fflush(stdin);
fwrite(&list, 1, sizeof(flight), flight_file);
fclose(flight_file);
//}
}
用户.h
#include <stdio.h>
#include <stdlib.h>
#include "booking.h"
void inputUser(FILE *input);
void inputUser(FILE *input) {
if( (input = fopen("users.dat", "wb") ) == NULL) {
printf("Error.");
}
else {
user travelers;
static int i = 0;
//for (int p = 0; p < 3; p++) {
printf("Name user %d: ", i+1);
scanf("%19s", travelers.name);
fflush(stdin);
printf("Surname user %d: ", i+1);
scanf("%19s", travelers.surname);
fflush(stdin);
printf("Day of birth user %d: ", i+1);
scanf("%hu", &travelers.date_of_birth.day);
fflush(stdin);
printf("Month of birth user %d: ", i+1);
scanf("%hu", &travelers.date_of_birth.month);
fflush(stdin);
printf("Year of birth user %d: ", i+1);
scanf("%hu", &travelers.date_of_birth.year);
fflush(stdin);
printf("Place of birth user %d: ", i+1);
scanf("%9s", travelers.place_of_birth);
fflush(stdin);
printf("Passport number user %d: ", i+1);
scanf("%8s", travelers.passport_number);
fflush(stdin);
fwrite(&travelers, 1, sizeof(user), input);
i++;
//}
fclose(input);
}
}
定义.h
#ifndef DEFINE_H_
#define DEFINE_H_
#define MAX_NAME 20
#define MAX_SURNAME 20
#define MAX_PASSPORT 9
#define MAX_PLACE_OF_BIRTH 15
#define MAX_FLIGHT_CODE 6
#define MAX_COMPANY_NAME 20
#define MAX_DEPARTURE 20
#define MAX_ARRIVAL 20
#define MAX_PLANE_CODE 5
#define MAX_SEATS 150
#define MAX_BOOKING_CODE 5
#endif /* DEFINE_H_ */
预期结果是结构 list,通过 fseek() 在函数 checkin() 中寻找(基本上选择) strong> 应该保存在结构 travelers.b.list 中,而当我尝试打印它时,我得到的只是各种符号等的混合。
我希望我为您弄清了一切,并为格式错误等提前道歉。
这对我来说是一个非常大的大学项目,我希望有人可以帮助我。
谢谢。
【问题讨论】:
-
您没有给出重现问题的方法,但我在回答中对代码的“完美”部分发表了一些评论(多次编辑,抱歉)。我不是在回答中说,而是将函数定义放在头文件中不是一个好主意,使用几个源文件
-
我建议永远不要将结构放入二进制文件中,因为它不可移植。以某种方式定义二进制文件,直接指定每个字节的值并进行所需的转换,可能在序列化库的帮助下。
-
@bruno .h 中的函数定义和相关 .c 中的函数本身是我们老师的选择。我对此无能为力。
-
@SlimShadys 通常我们将函数声明放在 h 文件中,而不是它们的定义中,您确定理解您的老师要求吗?
-
@aschepler 你能详细说明你的答案吗?你的意思是我不应该将我的结构保存在二进制文件中?如果是这样,我如何在文件本身中移动指针并进行所需的编辑? (由于管理员可以更改航班时间,或者公司名称,例如)
标签: c file struct binaryfiles fwrite