30/09/2018, 19:04

File trong C ạ!

Các anh chị pro C cho em hỏi chút về cái đoạn code của em đáng nhẽ khi ấn ‘Ý’ thì nó phải ghi lại hết nhưng của em thì khi thoát xông vào lại mà in thì nó toàn là 0.

struct {
	char ht[20];
	int MSSV;
	char lop[10];
	struct{
		int ng,tg,na;
	}ngaysinh;
	char gt[10];
	float diem;
}hs[200],tg;
FILE *f;
int n;
int kt=sizeof(tg);
void nhapdulieu();
void indulieu();
void sapxep();
void diemthi();
main()
{
f=fopen("data5.txt","rb");
if(f!=NULL)
{
fseek(f,0,2);
n=ftell(f)/kt;
rewind(f);
}
	char ch;
	int i,j;
//	tiep :
fread(hs,kt,n,f);
do
{		
	printf("
                     Chose ont option . Press 1->5 to chose 


");
	printf("
                     1.Nhap du lieu. ");
	printf("
                     2.In du lieu. ");
	printf("
                     3.Sap xep du lieu da nhap. ");
	printf("
                     4.Diem thi tb theo lop. ");
	printf("
                     5.Ket thuc thao tac. ");
	printf("                       
");
	ch=getch();
switch(ch)
{
case '1':
{	system("cls");
	nhapdulieu();
	break;
	}	
case '2':
{	system("cls");
	indulieu();
	break;
	}
case '3':
{	system("cls");
	sapxep();
	break;
		}
case '4':
{	system("cls");
	diemthi();
		break;		}
case '5':
	{
	printf("
 Are you sure want to exit ?? Press Y/N to exit(continue)");
	fflush(stdin);
	if((ch=getch())=='Y')
	{	
		fclose(f);
		f=fopen("data5.txt","rb");
		fwrite(hs,kt,n,f);
		fclose(f);
		printf("
 Good bye !");
		return 0;
	}
	break;
}
}
system("cls");
}while(1);
}
void nhapdulieu()
{  
//	char ht[25];
//	char gt[10];
//	char lop[15];
	do
	{	
		printf("
 Vao ten cua sinh vien thu %d ",n+1);
		printf("
 Ho ten :");
		fflush(stdin);
		gets(hs[n].ht);
		if(hs[n].ht[0]==0) break;
		printf("
 MSSV :");
		scanf("%d",&hs[n].MSSV);
		printf("
 Lop :");
		fflush(stdin);
		gets(hs[n].lop);
		printf("
 Ngay sinh :");
		scanf("%2d/%2d/%4d",&hs[n].ngaysinh.ng,&hs[n].ngaysinh.tg,&hs[n].ngaysinh.na);
		printf("
 Gioi tinh :");
		fflush(stdin);
		gets(hs[n].gt);
		printf("
 Diem :");
		scanf("%f",&hs[n].diem);
	//	fprintf(f,"%17s%6d/%2d/%4d%15d%7s%7s%15f
",hs[n].ht,hs[n].ngaysinh.ng,hs[n].ngaysinh.tg,hs[n].ngaysinh.na,hs[n].MSSV,hs[n].gt,hs[n].lop,hs[n].diem);
		n++;
	}while(n<200);
	//	fwrite(hs,sizeof(hs),n,f);
	printf("
 Da nhap du so luong ho so yeu cau 
");
}
void indulieu()
{	int i=0;

	printf("
          Quan Ly Diem Thi");
		printf("
| STT |     Ho va ten    |  Ngay sinh  |    MSSV   | Gioi tinh | Lop | Diem thi |");
	for(i=0;i<n;i++)
	//{	/*fflush(stdin);
		//fscanf(f,"%s%d%d%d%d%s%7s%f",&hs[i].ht,&hs[i].ngaysinh.ng,&hs[i].ngaysinh.tg,&hs[i].ngaysinh.na,&hs[i].MSSV,&hs[i].gt,&hs[i].lop,&hs[i].diem);*/
		{printf("
%4d%17s%6d/%2d/%4d%15d%7s%7s%15f",i+1,hs[i].ht,hs[i].ngaysinh.ng,hs[i].ngaysinh.tg,hs[i].ngaysinh.na,hs[i].MSSV,hs[i].gt,hs[i].lop,hs[i].diem);
	}
	getch();
}
int timkiem(char *s)
{	//fread(hs,sizeof(hs),n,f);	
	int i;
	int l;
	l=strlen(s);
	for(i=l-1;i>=0;i--)
	{
		if(s[i]!=' '&&l>0) l--;
	}
	return l;
}
void hoanvi(int i,int j)
{	
	tg=hs[i];
	hs[i]=hs[j];
	hs[j]=tg;
}
void sapxep()
	{//fread(hs,sizeof(hs),n,f);	 
	int i,j;	
for(i=0;i<200;i++)
{
	for(j=i+1;j<n;j++)
	{
	if(strcmp(hs[i].lop,hs[j].lop)>0)
	{
		hoanvi(i,j);
	}
	else
	{
		if(strcmp(hs[i].lop,hs[j].lop)==0)
		{
			if(strcmp(hs[i].ht+timkiem(hs[i].ht),hs[j].ht+timkiem(hs[j].ht))>0)
			{
				hoanvi(i,j);
			}
			else
			{
				if(strcmp(hs[i].ht,hs[j].ht)>0)
				{
					hoanvi(i,j);
				}
			}
		}
	}
}
}
printf("
 Du lieu da duoc sap xep");
getch();	
}	
void diemthi()
{int i;	
char lp[10];
	float diem=0;
	int dem=0;
	printf("
 Nhap vao lop muon xem :");
	fflush(stdin);
	gets(lp);
	printf("Diem trung binh cua lop do la :");
	for(i=0;i<200;i++)
	{
		if(strcmp(hs[i].lop,lp)==0)
		{
		diem+=hs[i].diem;
		dem++;
			}
			}
	printf("%f",diem/dem);
	getch();									
											}
																						
Bài liên quan
0