gpt4 book ai didi

c - 恐惧必须井然有序吗?

转载 作者:太空宇宙 更新时间:2023-11-04 04:32:43 27 4
gpt4 key购买 nike

所以我正在为一个程序编写代码,该程序具有多个菜单,这些菜单将不同的结构数据写入一个文件,然后另一个菜单显示这些文件中写入的数据。这是菜单的代码:

void displayall()
{
FILE *fp;
int choice=0;
struct depart loc = {0};
struct arrive loc1 = {0};
struct travel trav = {0};
fp=fopen("traveldat.dat","r");

while (1)
{
fread(&loc,sizeof(loc),1,fp);
fread(&loc1,sizeof(loc1),1,fp);
fread(&trav,sizeof(trav),1,fp);
double distance,time;
distance = sqrt(pow((loc1.x2-loc.x1),2)+pow((loc1.y2-loc.y1),2));
time = distance/trav.spd;

if (feof(fp))
{
break;
}
printf("\tYour departure location is : %s\n",loc.dep);
printf("\tWith the x-coordinate : %.2f\n",loc.x1);
printf("\tAnd the y-coordinate : %.2f\n\n",loc.y1);
printf("\tYour destination location is : %s\n",loc1.dest);
printf("\tWith the x-coordinate : %.2f\n",loc1.x2);
printf("\tAnd the y-coordinate : %.2f\n\n",loc1.y2);
printf("\tThe distance between these two locations is : %.2fkm\n\n",distance);
printf("\tYour preferred travel method is : %s\n",trav.mthd);
printf("\tWhich has a top speed of : %.2f km/h\n\n",trav.spd);
printf("\tYour expected travel time is : %.2f hours*\n\n",time);
printf("\t*Estimation,actual travel times may vary depending on certain conditions\n\n");
printf("\tThe system will now display the Main Menu\n\n");
}
fclose(fp);
}

我面临的问题是,如果我在写loc的菜单之前转到写loc1或trav的菜单,显示菜单不起作用,返回到主菜单,然后拒绝打开任何时候我尝试访问它。是因为 fread(&loc) 位于其他 freads 之前吗?还是我缺少什么?如果这段代码有碍眼或者我问错了,请提前致歉,我才学编程一个月左右。

根据要求编辑:loc1 和 loc 代码

void arrival_location_menu()
{
FILE *fp;
int choice=0;
struct arrive loc1;
fp=fopen("traveldat.dat","a");
printf("Please select your option (Destination location)\n");
printf("1.HOME\n");
printf("2.Andromeda Galaxy\n");
printf("3.The Library\n");
printf("4.Cprogramming.com\n");
printf("5.Return to main menu\n");
scanf("%d",&choice);
fflush (stdin);

switch (choice)
{
case 1: loc1.x2 = 3750;
loc1.y2 = 3450;
loc1.dest = "HOME";
system("CLS");
break;

case 2: loc1.x2 = 9870;
loc1.y2 = 5660;
loc1.dest = "Andromeda Galaxy";
system("CLS");
break;

case 3: loc1.x2 = 1367;
loc1.y2 = 3123;
loc1.dest = "The Library";
system("CLS");
break;

case 4: loc1.x2 = 2133;
loc1.y2 = 4767;
loc1.dest = "stackoverflow.com";
system("CLS");
break;

case 5: system("CLS");
break;

default: printf("Invalid option! Returning you to main menu...\n");
}
fwrite(&loc1,sizeof(loc1),1,fp);

fclose(fp);
return;
}


//DEPARTURE MENU
void departure_location_menu()
{
FILE *fp;
int choice=0;
struct depart loc;
fp=fopen("traveldat.dat","w");
printf("Please select your option (Departure location)\n");
printf("1.UTAR\n");
printf("2.PLUTO\n");
printf("3.IDK\n");
printf("4.UMM\n");
printf("5.Return to main menu\n");
scanf("%d",&choice);
fflush (stdin);

switch (choice)
{
case 1: loc.x1 = 1738;
loc.y1 = 1997;
loc.dep = "UTAR";
system("CLS");
break;

case 2: loc.x1 = 9850;
loc.y1 = 5675;
loc.dep = "PLUTO";
system("CLS");
break;

case 3: loc.x1 = 1363;
loc.y1 = 3125;
loc.dep = "IDK";
system("CLS");
break;

case 4: loc.x1 = 2130;
loc.y1 = 4785;
loc.dep = "UMM";
system("CLS");
break;

case 5:
system("CLS");
break;

default: printf("Invalied option!\n");
}
fwrite(&loc,sizeof(loc),1,fp);

fclose(fp);
return;
}

最佳答案

您似乎在询问是否可以从文件中的任何位置读取数据。是的,你可以。

有一个函数叫做 fseek()调整文件指针。文件指针也是下一次读取或写入的位置。

还有一个函数叫做ftell()读取当前文件指针。如果您要更改文件指针并希望稍后恢复它,这一点很重要。

我还建议您养成初始化变量(即使是 NULL 或零)和检查函数返回值的习惯。这两件简单的事情可以使调试变得非常简单。

我相信用户@m-m 已经解释了编码逻辑错误。

关于c - 恐惧必须井然有序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34130167/

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