| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3458 人关注过本帖
标题:各位帮我看一下为什么输出的结果总是“文件打开失败”
只看楼主 加入收藏
编程小白兔
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-5-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
各位帮我看一下为什么输出的结果总是“文件打开失败”
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define NUM 10
typedef struct
{
  char driver[10];
  int car;
  char police[10];
  int num;
  int year;
  int month;
  int date;
} Traffic;
int menu_select();
int Input(Traffic trafList[],int n);
void Display(Traffic trafList[],int n);
int Delete_a_record(Traffic trafList[],int n);
void Sort_by_Name(Traffic trafList[],int n);
int Insert_a_record(Traffic trafList[],int n);
int AddfromText(Traffic trafList[],int n,char *filename);
void WritetoText(Traffic trafList[],int n,char *filename);
void Query_a_record(Traffic trafList[],int n);



void main()
{
    Traffic trafList[NUM];
    int n=0;
    FILE*fp;
    int i;
    fp=fopen("records.txt","r");
    if(fp==NULL)
    {    printf("文件打开失败!\n");
        exit(1);
    }
    fprintf(fp,"%d",n);
    printf("%d",n);
    for(i=0;i<1;i++)
    {
        fprintf(fp,"%s\t\t",trafList[i].driver);   
        fprintf(fp,"%d\t",trafList[i].car);   
        fprintf(fp,"%s",trafList[i].police);   
        fprintf(fp,"%d\t",trafList[i].num);   
        fprintf(fp,"%d\n",trafList[i].year);   
        fprintf(fp,"%d\t",trafList[i].month);   
        fprintf(fp,"%d\t",trafList[i].date);
    }
    for(;;)
    {
        switch(menu_select())
        {
          case 1:
              n=Input(trafList,n);
              system("pause");
              break;
          case 2:
               Display(trafList,n);
               system("pause");
               break;
          case 3:
               n=Delete_a_record(trafList,n);
              system("pause");
              break;
           case 4:
              Sort_by_Name(trafList,n);
              system("pause");
              break;
           case 5:
              
              n=Insert_a_record(trafList,n);
              system("pause");
              break;
          case 6:
              
              Query_a_record(trafList,n);
              system("pause");
              break;
          case 7:
               n=AddfromText(trafList,n,"data.txt");
              system("pause");
              break;
          case 8:
              WritetoText(trafList,n,"records.txt");
              system("pause");
              break;
          case 0:
            printf("退出\n");
            system("pause");
            exit(0);
        }
    }
}




int menu_select()
{
  char c;
  do {
      system("cls");
      printf("1.Input records\n");
      printf("2.Display all records\n");
      printf("3.Delete a record\n");
      printf("4.Sort\n");
      printf("5.Insert a record\n");
      printf("6.Query\n");
      printf("7.Add records form a Text File\n");
      printf("8.Write to a next Text File\n");
      printf("0.Quit\n");
      printf("Give your choice(0-8)");
      c=getchar();
  }while(c<'0'||c>'8');
   return c-'0';
}
int Input(Traffic trafList[],int n)
{
  char ch;  
  printf("继续吗?Y or N?\n");
  getchar();
  ch=getchar();
  while(ch=='Y')
  {
      printf("请输入司机姓名");
      scanf("%s",trafList[n].driver);
      printf("请输入车牌号");
      scanf("%d",&trafList[n].car);
      printf("请输入开罚单交警姓名");
      scanf("%s",trafList[n].police);
      printf("请输入罚单编号");
      scanf("%d",&trafList[n].num);
      printf("请输入开罚单的年");
      scanf("%d",&trafList[n].year);
      printf("请输入开罚单的月");
      scanf("%d",&trafList[n].month);
      printf("请输入开罚单的日期");
      scanf("%d",&trafList[n].date);
      printf("继续吗?Y or N\n");
      getchar();
      ch=getchar();
      n++;
  }
 return n;
}
void Display(Traffic trafList[],int n)
{
    int i;
 for(i=0;i<n;i++)
 {
     printf("司机姓名:%s\t车牌号:%d\t交警姓名:%s\t罚单编号:%d\t开罚单的年:%d\t开罚单的月:%d\t开罚单的日期:%d\n",trafList[i].driver,trafList[i].car,trafList[i].police,trafList[i].num,trafList[i].year,trafList[i].month,trafList[i].date);
    // if(i%10==0) system("pause");
 }
}
int Delete(Traffic trafList[],int n,char *name)
{
     int i,j;
     for(i=0;i<n;i++)
     {
         if(strcmp(trafList[i].driver,name)==0)
         { for(j=i;j<n;j++)
           trafList[j]=trafList[j+1];
         }
     }
     n--;
     return n;
}
int Delete_a_record(Traffic trafList[],int n)
{
  char s[10];
  char zq;
    printf("请输入待删者姓名:\n");
    scanf("%s",&s);
    printf("您真要删除姓名为%s的记录吗?Y or N\n",s);
    scanf("%s",&zq);
//    zq=getchar();
    if(zq=='Y')
    {
        n=Delete(trafList,n,s);
        printf("您已经成功删除记!\n");
    }
    return n;
}
void Sort_by_Name(Traffic trafList[],int n)
{
    int i,j,p;
    Traffic zq;
    //printf("N=%d\n",n);
    for(i=0;i<n;i++)
    {
        p=i;
        for(j=i+1;j<n;j++)
        if(strcmp(trafList[j].driver,trafList[p].driver)<0) p=j;
        if(p=!i)
        {
           zq=trafList[p];trafList[p]=trafList[i];trafList[i]=zq;
        }
    }
}
int Insert(Traffic trafList[],int n,Traffic *s)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        //printf("%d",n);

     if(strcmp(s[i].driver,trafList[i].driver)<0)
     {
         for(j=n;j>0;j--)
             trafList[j]=trafList[j-1];
         trafList[i]=*s;
         break;
     }
    }
    if(i==n) trafList[n]=*s;
    n++;
    return n;
}
int Insert_a_record(Traffic trafList[],int n)
{
    Traffic zq;
    printf("请输入带插入司机姓名:\n");
    scanf("%s",zq.driver);
    printf("请输入驾驶的车牌号:\n");
    scanf("%d",&zq.car);
    printf("请输入开罚单的交警姓名:\n");
    scanf("%s",zq.police);
    printf("请输入罚单编号:\n");
    scanf("%d",&zq.num);
    printf("请输入开罚单年份:\n");
    scanf("%d",&zq.year);
    printf("请输入开罚单月份:\n");
    scanf("%d",&zq.month);
    printf("请输入开罚单日期:\n");
    scanf("%d",&zq.date);
    n=Insert(trafList,n,&zq);
    printf("该记录已经输入成功\n");
    return n;
}
int Query(Traffic trafList[],int n,char *name)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(strcmp(trafList[i].driver,name)==0)
            return i;
    }
    if(i==n)    return -1;
}
void Query_a_record(Traffic trafList[],int n)
{
    int a;
    char zq[10];
    printf("请输入待查找者的姓名:\n");
    scanf("%s",zq);
    a=Query(trafList,n,zq);
    if(a=!-1)
    {
        printf("查找成功!该司机的罚单信息如下:\n");
        printf("司机姓名:%s\n车牌号:%d\n交警姓名:%s\n罚单编号%d\n年份:%d\n月份:%d\n日期:%d\n",trafList[a].driver,trafList[a].car,trafList[a].police,trafList[a].num,trafList[a].year,trafList[a].month,trafList[a].date);
    }
    else printf("查找失败!\n");
}
int AddfromText(Traffic trafList[],int n,char *filename)
{
    FILE *fp;Traffic zq[NUM];
    int z,i;
    if((fp=fopen(filename,"r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(1);
    }
    fscanf(fp,"%d",&z);
    //printf("z=%d\n",z);
    for(i=0;i<z;i++)
    {
        fscanf(fp,"%s",zq[i].driver);
        fscanf(fp,"%d",&zq[i].car);
        fscanf(fp,"%s",zq[i].police);
        fscanf(fp,"%d",&zq[i].num);
        fscanf(fp,"%d",&zq[i].year);   
        fscanf(fp,"%d",&zq[i].month);   
        fscanf(fp,"%d",&zq[i].date);
    }
    for(i=0;i<z;i++,n++)
    //trafList[n]=zq[i];
    Insert(trafList,n,zq);
    return n;
}
void WritetoText(Traffic trafList[],int n,char *filename)
{
    FILE *fp;
    int i;
    fp=fopen("records.txt","W");
    if(fp==NULL)
    {
        printf("文件打开失败!\n");
        exit(1);
    }
    fprintf(fp,"%d\n",n);
    for(i=0;i<n;i++)
    {
        fprintf(fp,"%s",trafList[i].driver);
        fprintf(fp,"%d\t",trafList[i].car);   
        fprintf(fp,"%s",trafList[i].police);   
        fprintf(fp,"%d\t",trafList[i].num);
        fprintf(fp,"%d\t",trafList[i].year);
        fprintf(fp,"%d\t",trafList[i].month);        
        fprintf(fp,"%d\n",trafList[i].date);
    }
    fclose(fp);
}
搜索更多相关主题的帖子: include police 
2014-05-03 23:52
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:4 
    fp=fopen("records.txt","r");
    if(fp==NULL)
    {    printf("文件打开失败!\n");
        exit(1);
    }
    fprintf(fp,"%d",n);
第一句是以什么方式打开文件?如果这个文件本来并不存在会是什么情况?
最后一句又是对文件进行什么操作?
要写程序,至少要掌握语言的基础,也要了解常用的函数。
2014-05-04 00:20
ditg
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:16
帖 子:852
专家分:1937
注 册:2014-4-10
收藏
得分:4 
fp=fopen("records.txt","r");

需要完整的路径名

梦想拥有一台龙芯3A-4000
2014-05-04 01:03
pycansi
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:5
帖 子:418
专家分:1060
注 册:2012-7-26
收藏
得分:4 
回复 3 楼 ditg
相对路经是可以的


莫问前尘有愧,但求今生无悔
2014-05-04 06:40
kwxx
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:309
专家分:913
注 册:2009-5-11
收藏
得分:4 
关键是你所指定的文件实际存在,而且文件名包括扩展名及路径都与你所指定的都一致。
fp=fopen("records.txt","r");  要求文件records.txt在你的C工程文件夹中。
2014-05-04 08:21
kwxx
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:309
专家分:913
注 册:2009-5-11
收藏
得分:0 
如果需要新建文件,打开方式用w;
fp=fopen("records.txt","w");  
2014-05-04 08:24
Andrew_Lee
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:185
专家分:626
注 册:2014-3-21
收藏
得分:4 
要是不确定文件是不是已经存在,那个fopen的标志用a就好了
2014-05-04 09:39
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:0 
以下是引用Andrew_Lee在2014-5-4 09:39:45的发言:

要是不确定文件是不是已经存在,那个fopen的标志用a就好了
似是而非,要用 +。
2014-05-04 12:28
快速回复:各位帮我看一下为什么输出的结果总是“文件打开失败”
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035828 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved