| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 705 人关注过本帖
标题:(求助)文件打不开是怎么回事?
只看楼主 加入收藏
wench001
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-5-20
收藏
 问题点数:0 回复次数:1 
(求助)文件打不开是怎么回事?
#include "stdio.h"
#include "string.h"
#define MAXN 300
#define MAX_NAME 20
 int len;
typedef struct student{
   int  num;
   char name[MAX_NAME];
   int  english;
   int  math;
   int  computer;

} STUDENT;

/*-----------input sub program---------*/

 input(STUDENT stu[])
{
   int no;
   char ch[MAX_NAME];
   int i=0 ;
   putchar('\n');
   printf("%s\n", "Enter the new records.And if you want exit, enter -1.");
   printf("%s\n", "format: number");
   printf("%s\n", "        name english math computer");
   putchar('\n');
   printf("such as:\n");
   printf("1\n");
   printf("Gates 100 100 100 100 \n");
   while(i<MAXN){
   printf("please input code :") ;
   scanf("%d", &no);
   if(no==-1)
   {len=i;
    break;
   }
   while(no !=0) {
     stu[i].num = no;
     scanf("%s%d%d%d", stu[i].name, &stu[i].english, &stu[i].math,&stu[i].computer);
     i++;
     scanf("%d",&no);
   }
   }
}


/*---------------output sub program---------------*/

void output(STUDENT stu[])
{
  int i;

  clrscr();
  printf("%8s", "number");
  printf("%8s", "name");
  printf("%8s", "english");
  printf("%8s", "math");
  printf("%10s", "computer");
  putchar('\n');

  for (i =0; i < 80; i++)
    putchar('=');
  putchar('\n');

  for (i = 0; i< len; i++)
  {
    printf("%8d", stu[i].num);
    printf("%8s", stu[i].name);
    printf("%8d", stu[i].english);
    printf("%8d", stu[i].math);
    printf("%10d", stu[i].computer);
    putchar('\n');
  }

  for (i =0; i < 80; i++)
    putchar('=');
  putchar('\n');
  printf("Press enter to continue.");
  getchar();
  getchar();
}
/*--------------------find sub program-----------------------*/

find(STUDENT stu[])
{
  int find_no, result ;
  int i;

  lab: result=0;
  printf("%s\n", "Enter the number which you want to find. If you want exit, enter -1.");
  scanf("%d", &find_no);
  if (find_no == -1) exit(1);     /*exit the fine sub program*/
  while( stu[result].num != find_no && result < len) result ++;
  if (result >= len ) {
    printf("%s\n", "There is no the record which you want.");
    goto lab;
  }
  else {
    clrscr();
    printf("%s\n", "The flowing record is you want.");

    for (i =0; i < 80; i++)
    putchar('=');
    putchar('\n');

    printf("%8s", "number");
    printf("%8s", "name");
    printf("%8s", "english");
    printf("%8s", "math");
    printf("%10s", "computer");
    putchar('\n');



    printf("%8d", stu[result].num);
    printf("%8s", stu[result].name);
    printf("%8d", stu[result].english);
    printf("%8d", stu[result].math);
    printf("%10d", stu[result].computer);

    putchar('\n');

    for (i =0; i < 80; i++)
    putchar('=');
    putchar('\n');

    goto lab;
  }
}


/*-----------------insert sub program-------------*/

 insert(STUDENT stu[])
{
  int no, pos, english, math, computer, i;
  char name[MAX_NAME];
  lab: printf("%s\n", "Enter the new records. If you want exit, enter -1.");
  printf("%s\n", "format: number");
  printf("%s\n", "        name english math computer average total");

  scanf("%d", &no);
  if (no == -1) return ;
  stu[len].num = no;

  scanf("%s%d%d%d", name,&english,&math,&computer);

  pos = 0;
  while ((stu[pos].math < math) && (pos < len) )
    pos ++;
  for (i = len-1; i >= pos; i--)
    stu[i+1] = stu[i];
  stu[pos].num = no;
  strcpy(stu[pos].name, name);
  stu[pos].english = english;
  stu[pos].math = math;
  stu[pos].computer = computer;

  len=len+1;
  goto lab;
}

/*--------------delete sub program--------------*/

delete(STUDENT stu[])
{
  int no, i, pos;

  lab: pos=0;
  printf("%s\n", "Enter the number which you want to delete. If you want exit, enter -1.");

  scanf("%d", &no);
  if (no == -1) return;
  while( (stu[pos].num != no) && (pos < len) )
    pos = pos +1;
  if (pos >= len) {
    printf("%s\n", "There is no the record which you want.");
    goto lab;
  }
  else {
    for (i = pos+1;i < len; i++)
      stu[i-1] = stu[i];
    len = len -1;
    if (len == 0) {
      printf("%s\n", "The number of records is zero, press enter to return.");
      getchar();
      getchar();
      return;
    }
  goto lab;
  }

}



/*------------write--------------*/
write(STUDENT stu[])
{ FILE *ft;
  if((ft=fopen("c:\\stu.dat","wb"))==NULL)
    {printf("!!error\n");
    }
  if(fwrite(stu,sizeof(STUDENT),len,ft)!=len)
    {printf("error\n");}
  fclose(ft);
}
/*------------load-------------*/
 load(STUDENT stu[])
 {
  int i;
  FILE *ft;
  if((ft=fopen("c:\\stu.dat","rb"))==NULL)
  {printf("read error!");
  }
  for(i=0;i!=feof(ft);i++)
   {fread(&stu[i],sizeof(struct student),1,ft);
    len++;
    }
    return len ;
 }


/*---------------manu------------*/

void paint()
{
  int i;

  clrscr();
  printf("%43s\n", "Manu");

  for (i = 0; i < 80; i++)
    putchar('=');
  putchar('\n');

  printf("             1 input                            2 output\n");
  printf("             3 find(number)                     4 insert \n");
  printf("             5 delete(number)                   6 write \n");
  printf("             7 read                             0 quit\n");
  for (i = 0; i<= 79; i++)
    putchar('=');
  putchar('\n');

  printf("%s\n", "Please enter the command:");

}

/*------------main program---------------*/

main()
{
  STUDENT stu[MAXN];
  char ctrl_ch;
  len=read(stu);
  paint();
  scanf("%c", &ctrl_ch);
  while (ctrl_ch != '0') {
    switch(ctrl_ch) {
      case '1':
 input(stu);
 break;
      case '2':
 output(stu);
 break;
      case '3':
 find(stu);
 break;
      case '4':
 insert(stu);
 break;
      case '5':
 delete(stu);
 break;
      case '6':
 write(stu);
  break;
    case '7':
 load(stu);
 break;
    default:
 if (ctrl_ch != '\n') printf("%s\n", "Error command.");
 break;
    }
    if (ctrl_ch != '\n') paint();
    scanf("%c", &ctrl_ch);
  }
}


搜索更多相关主题的帖子: int 文件 STUDENT input 
2008-06-13 19:56
leilei88
Rank: 2
来 自:青岛
等 级:论坛游民
帖 子:217
专家分:27
注 册:2008-3-30
收藏
得分:0 
来点语言表达行不行啊。。这么一大堆代码谁愿意去看啊。。
2008-06-13 22:59
快速回复:(求助)文件打不开是怎么回事?
数据加载中...
 
   



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

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