| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 599 人关注过本帖
标题:各位帮我看一下为什么输出的结果总是“文件打开失败”
只看楼主 加入收藏
编程小白兔
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-5-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
各位帮我看一下为什么输出的结果总是“文件打开失败”
#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
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:10 
简单的看了一下:

你这个程序貌似运行的前提是

    fp=fopen("records.txt","r");    //这个文件必须事先得存在
    if(fp==NULL)
    {    printf("文件打开失败!\n");
        exit(1);
    }
   
解决方式:1、你事先把这个文件模板做好了,放在预定的目录下
          2、设置标志 该程序是首次运行 还是非首次运行,首次运行 则创建文件 非首次运行 则打开文件
另:不要重复发帖,重复发帖并不会增加你解决问题的几率

DO IT YOURSELF !
2014-05-04 09:21
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:10 
文件如果不存在肯定打不开的!

我不是砖家,要努力成为砖家。
2014-05-04 14:12
编程小白兔
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-5-3
收藏
得分:0 
回复 2 楼 wp231957
谢谢啦,不过我还是不怎么会弄,求指导
2014-05-04 16:50
编程小白兔
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-5-3
收藏
得分:0 
回复 2 楼 wp231957
问题已经解决,谢谢
2014-05-04 17:29
快速回复:各位帮我看一下为什么输出的结果总是“文件打开失败”
数据加载中...
 
   



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

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