各位大哥帮忙看看...从文件里提取数据的时候cannot open file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#define SIZE 200
int flag;
struct flight /*定义结构体*/
{
char distination[10];
char price[5];
char month[5];
char day[5];
char year[5];
char hour[5];
char minute[5];
}flights[SIZE];
void save() /*把结构体的内容保存到文件中*/
{
FILE *fp;
int i=0;
if((fp=fopen( "flightfile.txt ", "w "))==NULL)
{
printf( "cannot open file\n ");
return;
}
for(i=0;i <SIZE;i++)
if(fwrite(&flights[i],sizeof(struct flight),1,fp)!=1)
{if(feof(fp)){fclose(fp);return;}
printf( "file read error\n ");
}
fclose(fp);
}
void load() /*把文件内容输出到结构体中*/
{
FILE *fp;
int i=0;
if((fp=fopen( "flightfile.txt ", "r "))==NULL);
{printf( "cannot open file\n ");return;}
while(feof(fp)!=1)
{
fread(&flights[i],sizeof(struct flight),1,fp);
i++;
return;
}
fclose(fp);
}
void input() /*从键盘向结构体输入信息*/
{ void winAdmin();
int i;
int j=1;
for(i=0;i <SIZE;i++)
if(j==1)
{ printf( "请输入航班的目的地:\n ");scanf( "%s ",flights[i].distination);
printf( "请输入机票的价格:\n ");scanf( "%s ",flights[i].price);
printf( "请输入航班的起飞日期(月份):\n ");scanf( "%s ",flights[i].month);
printf( "请输入航班的起飞日期(日期):\n ");scanf( "%s ",flights[i].day);
printf( "请输入航班的起飞日期(年份):\n ");scanf( "%s ",flights[i].year);
printf( "请输入航班的起飞时间(几点):\n ");scanf( "%s ",flights[i].hour);
printf( "请输入航班的起飞时间(几分):\n ");scanf( "%s ",flights[i].minute);
printf( "您还想再输入新的航班信息吗?(1.是/2.否)\n ");
scanf( "%d ",&j);
flag++;
}
save();
winAdmin();
}
void output() /*打印结构体内容*/
{
int i;
printf( "目的地 票价 月 日 年 时间 \n ");
for(i=0;i <flag;i++)
printf( "%s %s %s %s %s %s:%s\n ",flights[i].distination,flights[i].price,flights[i].month,flights[i].day,flights[i].year,flights[i].hour,flights[i].minute);
return;
}
void winGuest() /*客户的欢迎界面*/
{void main();
void search();
int choice;
loop:printf( "\n\n\n\n\n\n\n\n "); /*为了在屏幕的中间位置显示菜单,加空行*/
printf( " |****** 欢迎光临SHEEP的航班查询系统 ******|\n ");
printf( " |---------------------------------------------|\n ");
printf( " | 请问您想做什么?(1~2): |\n ");
printf( " |---------------------------------------------|\n ");
printf( " | 1----搜索航班信息 |\n ");
printf( " | 2----回到主菜单 |\n ");
printf( " | 3----退出 |\n ");
printf( " |---------------------------------------------|\n ");
scanf( "%d ",&choice);
switch(choice)
{
case 1:search();break;
case 2:main();break;
default:printf( "您输入的选项不存在,请重新输入:\n ");goto loop;
}
}
void search() /*搜索航班信息*/
{ void screening();
int c;
load();
output();
loop:printf( "\n\n\n************************************\n 您想按目的地查找航班信息吗(1.是 2.否)? ");
scanf( "%d ",&c);
if(c==1)
screening();
else
if(c==2)
{ printf( "按任意键离开 ");
getchar();
main();
}
else
{
printf( "该选项不存在,请重新选择 ");
goto loop;
}
}
void screening()
{
int i=0,j;
char dist[10]={0};
printf( "请输入您要查询的目的地 ");
scanf( "%s ",dist);
printf( "目的地 票价 月 日 年 时间 \n ");
for(j=0;j <10;j++)
if(strcmp(flights[j].distination,dist)==0)
{
printf( "%s %s %s %s %s %s:%s ",flights[j].distination,flights[j].price,flights[j].month,flights[j].day,flights[j].year,flights[j].hour,flights[j].minute);
i++;
}
if(i==0)
{
printf( "没有您要查找的内容,请按任意键离开 ");
getchar();
main();
}
}
void ticketimfo()
{
}
void winAdmin() /*管理员的欢迎界面*/
{
void main();
int choice;
loop:printf( "\n\n\n\n\n\n\n\n "); /*为了在屏幕的中间位置显示菜单,加空行*/
printf( " |****** 欢迎光临SHEEP的航班查询系统 ******|\n ");
printf( " |---------------------------------------------|\n ");
printf( " | 请问您想做什么?(1~4): |\n ");
printf( " |---------------------------------------------|\n ");
printf( " | 1----搜索航班信息 |\n ");
printf( " | 2----输入新的航班信息 |\n ");
printf( " | 3----查询机票订购情况 |\n ");
printf( " | 4----回到主菜单 |\n ");
printf( " | |\n ");
printf( " |---------------------------------------------|\n ");
scanf( "%d ",&choice);
switch(choice)
{
case 1:search();break;
case 2:input();break;
case 3:ticketimfo();break;
case 4:main();break;
default:printf( "您输入的选项不存在,请重新输入:\n ");goto loop;
}
}
void main()
{
int id,t=0;
do{ printf( "\n\n\n\n\n\n\n\n ");
printf( " |****** 欢迎光临SHEEP的航班查询系统 ******|\n\n ");
printf( " |请问您是?(1~2): |\n ");
printf( " |---------------------------------------------|\n ");
printf( " | 1----系统管理员 |\n ");
printf( " | 2----乘客 |\n ");
printf( " | |\n ");
printf( " | |\n ");
printf( " |---------------------------------------------|\n ");
scanf( "%d ",&id);
if(id==1)
winAdmin();
else if(id==2)
winGuest();
else
printf( "the choice does not exist,please input again:\n ");
}while((id!=1)&&(id!=2));
}
[解决办法]
if((fp=fopen( "flightfile.txt ", "r "))==NULL);
__________________________________________________
flightfile.txt这个文件是否存在?
[解决办法]
文件打不开,可能是文件损坏了,也可能文件根本就不存在,首先确保你的文件已经成功的写入,在硬盘上存在.如果文件不存在,或者数据与你写入的数据不一致,那么问题就在你写入的时候有错误发生,仔细检查那里就好了.
void save() /*把结构体的内容保存到文件中*/
{
FILE *fp;
int i=0;
if((fp=fopen( "flightfile.txt ", "w "))==NULL)
{
printf( "cannot open file\n ");
return;
}
for(i=0;i <SIZE;i++)
if(fwrite(&flights[i],sizeof(struct flight),1,fp)!=1)
{if(feof(fp)){fclose(fp);return;}
printf( "file read error\n ");
}
fclose(fp);
}
你把结构体写入文件的程序不理解为什么要这样做,在写文件的时候,为什么要判断是否到了文件尾?{if(feof(fp)){fclose(fp);return;}