gpt4 book ai didi

c - C语言更新记录

转载 作者:行者123 更新时间:2023-11-30 21:10:38 24 4
gpt4 key购买 nike

有人可以帮我处理这段代码吗?当我尝试这段代码时,它根本不起作用。我在使用 C 语言处理文件方面的经验非常少。请帮我解决这个问题

结构如下

struct passenger
{
char airline[20];
char name[20];
int age;
char gender;
char clas[10];
int seatNo;

}e;

这里是插入记录的函数

oid reserve()
{
int airline, clas;
printf("\nSelect your flight:");
printf("\n1. PIA\n2. Shaheen\n3. Air Blue\n\nEnter Choice:");
scanf("%d", &airline);
if (airline == 1)
strcpy(e.airline, "\nPIA");
else if (airline == 2)
strcpy(e.airline, "\nShaheen");
else if (airline == 3)
strcpy(e.airline, "\nAirBlue");

printf("\nEnter Name:");
scanf("%s", &e.name);

printf("\nEnter Age:");
scanf("%d", &e.age);

printf("\nEnter Your Gender (M/F): ");
e.gender = getche();

printf("\n\nPress 1 for ECONOMY CLASS and 2 for BUSSINESS CLASS: ");
scanf("%d", &clas);
if (clas == 1)
strcpy(e.clas, "Economy");
else
strcpy(e.clas, "Bussiness");
e.seatNo = rand() % 500;
printf("\nYour seat num is: %d\n\n", e.seatNo);

if (airline==1)
save = fopen("PIA.DAT", "a");
else if (airline==2)
save = fopen("Shaheen.DAT", "a");
else
save = fopen("AirBlue.DAT", "a");
fwrite(&e, sizeof( struct passenger), 1, save);
fclose(save);

getch();
}

这是为了更新记录

void update()
int i = 1, count = 0, clas, record = 0;
int seat, airline;
struct passenger se;
printf("\n\n\nSelect your flight:");
printf("\n1. PIA\n2. Shaheen\n3. Air Blue\n\nEnter Choice:");
scanf("%d", &airline);

if (airline == 1)
updateR = fopen("PIA.DAT", "r+");
else if (airline == 2)
updateR = fopen("Shaheen.DAT", "r+");
else
updateR = fopen("AirBlue.DAT", "r+");

if (updateR == NULL)
{
printf("File not found.");
}
else
{
printf("\nEnter Your Seat No: ");
scanf("%d", &seat);
while (!feof(updateR))
{
fread(&se, sizeof(struct passenger), 1, updateR);
if (se.seatNo == seat)
{
printf("\nEnter Name:");
scanf("%s", &e.name);

printf("\nEnter Age:");
scanf("%d", &e.age);

printf("\nEnter Your Gender (M/F): ");
e.gender = getche();

printf("\n\nPress 1 for ECONOMY CLASS and 2 for BUSSINESS CLASS: ");
scanf("%d", &clas);
if (clas == 1)
strcpy(e.clas, "Economy");
else
strcpy(e.clas, "Bussiness");
fseek(updateR,sizeof(struct passenger)* record, SEEK_END);
fwrite(&e, sizeof(&e), 1, updateR);
break;
}
}
fclose(updateR);
}
getch();
}

最佳答案

缺少相当多的信息,无法进行全面分析。但乍一看,您是从文件末尾查找。您正在使用SEEK_END。通常您应该使用 SEEK_SET。

关于c - C语言更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28864056/

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