ktltbai34.cpp

#include<stdio.h>
    
//=== Chuong trinh chinh ===
int main(){
    FILE *f;
    float x;
	// Nhập và lưu các số thực vào file
    f = fopen("sothuc.dat","wb");
    do{
		printf("Nhap vao so thuc (nhap 0 neu muon dung): ");
		scanf("%f",&x);
		if(x!=0)
			fwrite(&x,sizeof(float),1,f); 
    }while(x!=0);
    fclose(f);  
	
	// Đọc file số thực và tính trung bình cộng
    f = fopen("sothuc.dat","rb");   
    float sum=0;
    int count =0;
    while(fread(&x,sizeof(float),1,f)>0){
		sum += x;
		count ++; 
		printf("%f\t",x);
    } 
    printf("\nTBC = %f",sum/count);
    fclose(f);
	// Ghi thêm 1 số thực vào file
	f = fopen("sothuc.dat","ab");
	printf("\nNhap vao so thuc : ");
	scanf("%f",&x);
	fwrite(&x,sizeof(float),1,f); 
    fclose(f); 
    
    return 0;

}

 //== Dinh nghia ham ==

Last updated