C 언어 간단한 예제(파일 읽기와 쓰기 7)

1147 단어
#include<stdio.h>

struct stu
{
	char name[10];
	int	num;
	int age;
	char addr[15];
}boya[2],boyb[2],*pp,*qq;

#define FILEPATH "D:\\test.txt"
void main()
{
	FILE *fp;
	char ch;
	int  i;
	pp = boya;
	qq = boyb;
	if((fp = fopen(FILEPATH,"wb+")) == NULL)
	{
		printf("Cannot open file strike any key exit!
"); getch(); exit(1); } printf("
input data
"); for(i = 0 ;i < 2; i++,pp++) { scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr); } pp = boya; for(i=0;i<2;i++,pp++) { fprintf(fp,"%s %d %d %s
",pp->name,pp->num,pp->age,pp->addr); } // fwrite(pp,sizeof(struct stu),2,fp); rewind(fp); for(i=0;i<2;i++,qq++) { fscanf(fp,"%s %d %d %s
",qq->name,&qq->num,&qq->age,qq->addr); } // fread(qq,sizeof(struct stu),2,fp); printf("

ame\tnumber age addr
"); qq = boyb; for(i = 0;i<2;i++,qq++) { printf("%s\t%5d%7d %s
",qq->name,qq->num,qq->age,qq->addr); } fclose(fp); getch(); }

좋은 웹페이지 즐겨찾기