20130826C语言编程实践课第六天

/****************************************************第一题********************************************************/
/*#include<stdio.h>
void main()
{
 FILE *infile,*outfile;
 char input;
 char inputs[10];

 infile=fopen("e:\\infile.txt","r+");
 outfile=fopen("e:\\outfile.txt","a+");
 if(!infile)
  printf("open infile failed…\n");
 if(!outfile)
  printf("open outfile failed…\n");
 for(;;)//while(1)
 {
  input=fgetc(infile);
  printf("c%",input);
  fputc(input,outfile);
  if(input==EOF)
   break;
 }
 fclose(infile);
 fclose(outfile);
}*/
/****************************************************第一题********************************************************/

 

/****************************************************第二题********************************************************/
#include<stdio.h>
#include<stdlib.h>
main()
{
 FILE *fp;
 int i;
 struct rec{//定义结构体类型
  char id[10];
  char name[15];
  char department[15];
 }record;
 fp=fopen("e:\\infile.txt","w");
 if(fp==NULL)//以文件只写方式打开文件
 {
  printf("can not open file");
  exit(1);
 }
 printf("please input data:\n");
 for(i=0;i<2;i++)
 {
  printf("Please input id:\n");
  scanf("%s",record.id);//从键盘输入
  printf("Please input name:\n");
  scanf("%s",record.name);
  printf("Please input department:\n");
  scanf("%s",record.department);
  fwrite(&record,sizeof(record),1,fp);//成块写入文件
  //fprintf(fp,"%s %s %s",record.id,record.name,record.department);
 }
 fclose(fp);//关闭文件
 fp=fopen("e:\\infile.txt","r");
 if(fp=NULL)//以文本只读方式重新打开文件
 {
  printf("cannot open file");
  exit(0);
 }
  printf("output from file :\n");//从文件 读入
  while(fscanf(fp,"%s %s %s",record.id,record.name,record.department)!=EOF)
  {
   printf("id:%s  name:30%s  department:%s\n ",record.id,record.name,record.department);
  }
  fclose(fp);//关闭文件
}
/****************************************************第二题********************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值