By GokiSoft.com| 15:43 10/12/2021|
Lập Trình C

[Video] Tìm hiểu về File trong C - Khóa học lập trình C - C2110I



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void testWriteFile() {
	//B1. Tao connection toi file & mo file (open file)
	FILE *fp;
	fp = fopen("vidu.txt", "a");
	
	//B2. Ghi noi dung du lieu vao file
	//ghi 1 ky tu vao FILE
	fputc('B', fp);
	fputs("\nSINH VIEN APTECH 54 LE THANH NGHI", fp);
	fprintf(fp, "\nLop C2110I");
	
	int x = 10;
	char s[50] = "Xin chao";
	fprintf(fp, "\nx = %d, s = %s", x, s);
	
	//B3. Dong ket noi toi file
	fclose(fp);
}

void testReadFile() {
	//B1. Mo file
	FILE *fp;
	fp = fopen("vidu.txt", "r");
	
	//B2. Doc noi dung
	/**
	int code = fgetc(fp);
	printf("\nKet qua code = %d, code = %c", code, code);
	char c = (char) code;
	printf("\nc = %c", c);
	*/
	/**char s[100];
	fgets(s, 10, fp);
	printf("\ns = %s", s);
	fgets(s, 10, fp);
	printf("\ns = %s", s);
	*/
	/**char s[100], s1[100];
	fscanf(fp, "%s%s", s, s1);
	printf("\ns = %s, s1= %s", s, s1);
	*/
	//TH -> dong toan bo noi dung trong file -> hien thi ra man -> cach 1 de hon
	int code;
	printf("NOI DUNG FILE\n");
//	for(;;) {
//		code = fgetc(fp);
//		if(code == -1) {
//			break;
//		}
//		printf("%c", code);
//	}
	while(!feof(fp)) {
		code = fgetc(fp);
		printf("%c", code);
	}
	
	//B3. Dong ket noi
	fclose(fp);
}

void testFullReadFile() {
	//B1. Mo file
	FILE *fp;
	fp = fopen("vidu.txt", "r");
	
	if(fp != NULL) {
		//TH mo file thanh cong
		int code;
		while(!feof(fp)) {
			code = fgetc(fp);
			printf("%c", code);
		}
	} else {
		//TH file khong ton tai -> gap loi gi do
		printf("\nFile Error!!!");
	}
	
	//B3. Dong ket noi
	fclose(fp);
}

typedef struct Student_ST {
	char name[50];
	int age;
} Student;

void testWriteFileBinary() {
	Student stdList[3];
	int i;
	
	for(i=0;i<3;i++) {
		printf("\nNhap thong tin sinh vien thu %d", i + 1);
		printf("\nNhap ten: ");
		fflush(stdin);fflush(stdout);
		gets(stdList[i].name);
		
		printf("\nNhap tuoi: ");
		scanf("%d", &stdList[i].age);
	}
	
	for(i=0;i<3;i++) {
		printf("\nten: %s, tuoi: %d", stdList[i].name, stdList[i].age);
	}
	
	//Luu thong tin vao file -> lam cach nao -> su dung luu binary
	//B1. Mo ket noi
	FILE *fp;
	fp = fopen("students.dat", "wb");
	
	//B2. Ghi noi dung vao file
	fwrite(stdList, sizeof(Student), 3, fp);
	
	//B3. Dong ket noi
	fclose(fp);
}

int main(int argc, char *argv[]) {
	//Lam viec voi FILE
	//Phan 1: Lam viec vs file text
	//1.1 Phan ghi noi dung
	//testReadFile();
	// 1.2 Doc noi dung trong file text
	//testReadFile();
	// 1.3 Code 1 chuong hoan thien -> kiem tra error
	//testFullReadFile();
	
	//Phan 2: File nhi phan (binary)
	// 2.1 -> Ghi noi dung file nhi phan
	//testWriteFileBinary();
	
	// 2.2 -> Doc noi dung file nhi phan
	Student stdList[3];
	int i;
	
	//B1. Mo ket noi
	FILE *fp;
	fp = fopen("students.dat", "rb");
	
	//B2. Doc noi dung vao file
	fread(stdList, 3, sizeof(Student), fp);
	//fwrite(stdList, sizeof(Student), 3, fp);
	
	//B3. Dong ket noi
	fclose(fp);
	
	//Hien thi
	for(i=0;i<3;i++) {
		printf("\nten: %s, tuoi: %d", stdList[i].name, stdList[i].age);
	}
	
	return 0;
}




Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó