Chapter 10 programs :
*******************************************************
#define TRUE 1
#include<stdio.h>appcrash
#include<stdlib.h>
struct personnel{
char name[40];
int agnumb;
double height;
};
struct personnel agent[50];
int n=0;
char numstr[40];
main(){
char ch;
while(TRUE){
printf("\n\'e\' enter new agent \n\'l\'list all agents");歌词
printf("\n\'w\' write file \n\'r\'read file\n\'o\'end the program:"); ch=getchar();getchar();
switch(ch){
ca 'e':newname();break;
ca 'l':listall();break;
ca 'w':wfile();break;
ca 'r':rfile();break;
ca 'o':exit(0);break;
default:puts("\nEnter only lections listed"); }
}
}
newname(){
printf("\nRecord %d \nEnter name:",n+1); gets(agent[n].name);
printf("Enter agent number(3 digit):");
gets(numstr);
agent[n].agnumb=atoi(numstr);
printf("Enter height in cm:");
gets(numstr);
agent[n++].height=atof(numstr);
}
listall(){
int j;
if(n<1)
printf("\nEmpty list.\n");
for(j=0;j<n;j++){
printf("\n Record number %d \n",j+1);
printf("Name:%s\n",agent[j].name);
printf("Agent nmuber:%03d\n",agent[j].agnumb); printf("Height:%4.2f\n",agent[j].height);
油炒面
丹参
}
}
wfile(){
FILE *fptr;
if(n<1){
printf("\nCannot write empty list.\n"); return;
}
if((fptr=fopen("","wb"))==NULL){
printf("\nCanot open \n"); return;
}
el
{
fwrite(agent,sizeof(agent[0]),n,fptr);
fclo(fptr);
printf("\nFile of %d records written.\n",n);
望湖楼醉书
}佛说人生感悟的句子
校运会稿件}
rfile(){
FILE *fptr;
int m=0;
if((fptr=fopen("","rb"))==NULL){
printf("\nCannot open \n");
}
el
{
while((fread(&agent[m],sizeof(agent[m]),1,fptr))==1)
m++;
fclo(fptr);
printf("\nFile read.Total agents is now %d.\n",m+1);
}
}
******************************************************* ******************************************************* #include<stdio.h>
main(){
struct{
char name[40];
int agnumb;
double height;
}agent;
FILE *fptr;
int recno;
long int offt;
if((fptr=fopen("","rb"))==NULL){ printf("Cannot open the \n"); exit(0);
}
printf("Enter record number:");
scanf("%d",&recno);
offt=(recno-1)*sizeof(agent);
if(fek(fptr,offt,0)!=0){
printf("cannot move pointer there.\n");
exit(0);
}
fread(&agent,sizeof(agent),1,fptr);
printf("\nName:%s\n",agent.name);
printf("Number:%03d\n",agent.agnumb); printf("Height:%-2f\n",agent.height);
白居易长恨歌fclo(fptr);
}