/****************************************************第一题********************************************************/
/*#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);//关闭文件
}
/****************************************************第二题********************************************************/