必须要谨慎吗?

时间:2015-12-07 09:23:20

标签: c file-io

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

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;
}

1 个答案:

答案 0 :(得分:0)

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

有一个名为fseek()的函数可以调整文件指针。文件指针也是下一个读取或写入的位置。

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

我还建议你养成初始化变量的习惯(即使它是NULL或零),以及检查函数的返回值。这两个简单的事情可以使调试变得更加简单。

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