Giải bài thực hành 10 – Tập tin | Nhập môn lập trình C

Giai bai thuc hanh 10 tap tin
DANH SÁCH BÀI VIẾT
Giải bài tập thực hành 1 – Các thành phần trong ngôn ngữ C
Giải bài tập thực hành 2 – Cấu trúc rẽ nhánh
Giải bài tập thực hành 3 – Cấu trúc lặp for
Giải bài thực hành 4 – Cấu trúc lặp While, do…while
Giải bài thực hành 5 – HÀM
Giải bài thực hành 6 – Truyền tham số cho hàm
Giải bài thực hành 7 – Mảng
Giải bài thực hành 8 – Chuỗi ký tự
Giải bài thực hành 9 – Kiểu cấu trúc
Giải bài thực hành 10 – Tập tin

Bài thực hành 10 – Tập tin

Bài 4. Viết chương trình tạo tập tin văn bản chứa 1 dãy số nguyên bất kỳ.

Code mẫu

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[100];
void randomList100()
{
	for(int i = 0;i<100;i++) a[i] = rand();
}
void ghiFile()
{
	FILE *f; 
	f=fopen("number.txt", "wt"); 
	if(f == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}

	for(int j=0; j<100; j++)
		fprintf(f, "%d\t", a[j]); 
	fclose(f);
}
int main()
{
	randomList100();
	ghiFile();
}

Bài 5. Viết chương trình tạo tập tin nhị phân chứa 10000 số nguyên bất kỳ ghi vào
file SONGUYEN.INP. Mỗi dòng 10 số, sau đó viết chương trình đọc file
SONGUYEN.INP, sắp xếp theo thứ tự tăng dần và lưu kết quả vào file
SONGUYEN.OUT.

Code mẫu

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10000

int a[MAX];
int readA[MAX];
void randomList100()
{
	for(int i = 0;i<MAX;i++) a[i] = rand();
}
void ghiFile()
{
	FILE *f; 
	f=fopen("SONGUYEN.INP.txt", "wt"); 
	if(f == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}

	for(int j=0; j<MAX; j++)
	{
		if(j%10==0) 	fprintf(f,"\n");
		fprintf(f, "%d\t", a[j]); 
	}
	fclose(f);
}
void docFILE()
{
	FILE *f; 
	f=fopen("SONGUYEN.INP.txt", "rt"); 
	if(f == NULL)
	{
		printf("\nFile Loi."); 
		exit(0); 
	}

	for(int j=0; j<MAX; j++)
	{
		fscanf(f, "%d", &readA[j]); 
	}
	fclose(f);
}
void sapXep()
{
	for(int i = 0;i<MAX-1;i++)
	{
		for(int j = i+1;j<MAX;j++)
		{
			if(readA[i] > readA[j])
				{
					int temp = readA[i];
					readA[i]  = readA[j];
					readA[j] = temp;
				}
		}
	}
}
void luuSapXep()
{
	FILE *f; 
	f=fopen("SONGUYEN.OUT.txt", "wt"); 
	if(f == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}
	sapXep();
	for(int j=0; j<MAX; j++)
	{
		if(j%10==0) 	fprintf(f,"\n");
		fprintf(f, "%d\t", readA[j]); 
	}
	fclose(f);
}
int main()
{
	randomList100();
	ghiFile();
	docFILE();
	luuSapXep();
	printf("HOAN THANH");
}

Bài 6. Viết chương trình tạo một file chứa 10000 số nguyên ngẫu nhiên đôi một
khác nhau trong phạm vi từ 1 đến 32767 và đặt tên là “SONGUYEN.INP”

Code mẫu

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10000
int a[MAX];
void randomList100()
{
	for(int i = 0;i<MAX;i++) {
		a[i] = rand() % 32767 ;
		for(int j = 0;j<i;j++) if(a[i] == a[j])
		{
			i--; break;
		}
	}
}
void ghiFile()
{
	FILE *f; 
	f=fopen("SONGUYEN.INP.txt", "wt"); 
	if(f == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}

	for(int j=0; j<MAX; j++)
		fprintf(f, "%d\t", a[j]); 
	fclose(f);
}
int main()
{
	randomList100();
	ghiFile();
	printf("HOAN THANH");
}

Bài 7. Viết chương trình tạo một file chứa các số nguyên có tên SONGUYEN.INP.
Sau đó đọc file SONGUYEN.INP và ghi các số chẵn vào file SOCHAN.OUT và
những số lẻ vào file SOLE.OUT.

Code mẫu

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10000

int a[MAX];
int readA[MAX];
void randomList100()
{
	for(int i = 0;i<MAX;i++) a[i] = rand();
}
void ghiFile()
{
	FILE *f; 
	f=fopen("SONGUYEN.INP.txt", "wt"); 
	if(f == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}

	for(int j=0; j<MAX; j++)
	{
		if(j%10==0) 	fprintf(f,"\n");
		fprintf(f, "%d\t", a[j]); 
	}
	fclose(f);
}
void docFILE()
{
	FILE *f; 
	f=fopen("SONGUYEN.INP.txt", "rt"); 
	if(f == NULL)
	{
		printf("\nFile Loi."); 
		exit(0); 
	}

	for(int j=0; j<MAX; j++)
	{
		fscanf(f, "%d", &readA[j]); 
	}
	fclose(f);
}
void luuChanLe()
{
	FILE *schan, *sle; 
	schan=fopen("SOCHAN.OUT.txt", "wt"); 
	sle=fopen("SOLE.OUT.txt", "wt"); 
	if(schan == NULL || sle == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}
	for(int j=0; j<MAX; j++)
	{
		if(readA[j] % 2 == 0)
			fprintf(schan, "%d\t", readA[j]);
		else fprintf(sle, "%d\t", readA[j]);
	}
	fclose(schan);fclose(sle);
}
int main()
{
	randomList100();
	ghiFile();
	docFILE();
	luuChanLe();
	printf("HOAN THANH");
}

Bài 8: Viết chương trình ghi vào tập tin SOCHAN.DAT các số nguyên chẵn từ 0
đến 100.

Code mẫu

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10000

void ghiFile()
{
	FILE *f; 
	f=fopen("SOCHAN.DAT.txt", "wt"); 
	if(f == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}

	for(int j=0; j<=100; j+=2)
		fprintf(f, "%d\t", j); 
	fclose(f);
}
int main()
{
	ghiFile();
	printf("HOAN THANH");
}

Bài 9. Viết chương trình đọc tập tin SOCHAN.DAT và xuất ra màn hình, mỗi dòng
30 số.

Code mẫu

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10000
int a[MAX];
int n = 0;
void docFile()
{
	FILE *f; 
	f=fopen("SOCHAN.DAT.txt", "rt"); 
	if(f == NULL)
	{
		printf("\nLoi."); 
		exit(0); 
	}
	
	while(!feof(f))
	{
		fscanf(f,"%d", &a[n]);
		n++;
	}
	fclose(f);
	for(int i = 0;i<n-1;i++)
	{
		if(i%30==0) printf("\n");
		printf("%d ", a[i]);
	}
}
int main()
{
	docFile();
	return 0;
}