Tập Tin C++

Sau đây bài viết về code C++ khi làm việc với file (tập tin)

  1. Chương trình minh hoạ cách tạo, mở một file và nhập một kí tự vào file
#include "stdio.h"

int main()
{
	FILE *f;
	f = fopen("Data.dat", "w");
	putc('C', f);
	fclose(f);

	return 0;
}
  1. Chương trình minh hoạ cách mở một tập tin đã tồn tại trên đĩa để đọc
#include "stdio.h"

int main()
{
	FILE *f;
	char ch;
	f = fopen("Data.dat", "r");
	ch = getc(f);
	printf("Ki tu la: %c\n", ch);

	fclose(f);

	return 0;
}
  1. Chương trình minh hoạ cách nhập một chuỗi kí tự vào tập tin
#include "stdio.h"
#include "conio.h"

int main()
{
	FILE *f;
	char ch;
	f = fopen("Data.dat", "w");
	while((ch = getche()) !=  '\r')
		ch = putc(ch, f);
	fclose(f);

	return 0;
}
  1. Chương trình minh hoạ cách đọc một chuỗi kí tự từ file đã tồn tại trên đĩa
#include "stdio.h"

int main()
{
	FILE *f;
	char ch;
	f = fopen("Data.dat", "r");
	while((ch = getc(f)) !=  EOF)
		printf("%c", ch);
	fclose(f);

	return 0;
}
  1. Chương trình sao chép 2 tập tin
#include "stdio.h"
#include "conio.h"

int main()
{
	FILE *f1, *f2;
	char ch;
	f1 = fopen("DataIn.dat", "r");
	f2 = fopen("DataOut.dat", "w");
	while((ch = getc(f1)) !=  EOF)
		putc(ch, f2);
	fclose(f1);
	fclose(f2);

	return 0;
}
  1. Chương trình tổng quát minh hoạ cách khởi tạo, nhập, xuất dữ liệu trên file
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"

int main()
{
	char selection[2];
	char file_name[13];
	char user_choice[2];
	int selection_value;
	int file_character;
	FILE *file_pointer;

	printf("Chon mot trong cac muc sau:\n");
	printf("1] Tao mot File moi.\n");
	printf("2] Viet de len mot file da ton tai.\n");
	printf("3] Cong them du lieu moi den file da ton tai.\n");
	printf("4] Doc du lieu tu file dang ton tai.\n");

	do {
		printf("Lua chon cua ban => ");
		gets(user_choice);
		selection_value = atoi(user_choice);
		switch(selection_value)
		{
		case 1:
		case 2:
			strcpy(selection, "w");
			break;
		case 3:
			strcpy(selection, "a");
			break;
		case 4:
			strcpy(selection, "r");
			break;
		default:
			printf("Dieu nay khong duoc chon.\n");
			selection_value = 0;
		}
	} while(selection_value == 0);

	printf("Nhap ten cua file => ");
	gets(file_name);

	if((file_pointer = fopen(file_name, selection)) == NULL)
	{
		printf("Khong mo duoc file %s", file_name);
		exit(-1);
	}

	switch(selection_value)
	{
	case 1:
		break;
	case 2:
	case 3:
		printf("Nhap vao chuoi de luu: \n");
		while ((file_character = getche()) != '\r')
			file_character = putc(file_character, file_pointer);
		break;
	case 4:
		while((file_character = getc(file_pointer)) != EOF)
			printf("%c", file_character);
		break;
	}
	fclose(file_pointer);

	return 0;
}
  1. Chương trình minh hoạ con trỏ file và kiểu cấu trúc
#include "stdio.h"
#include "conio.h"

typedef struct
{
	char part_name[15];
	int quantity;
	float cost_each;
} parts_structure;

int main()
{
	parts_structure parts_data;
	FILE *file_pointer;

	file_pointer = fopen("PARTS.DAT", "wb");
	do {
		printf("\nName of part => ");
		gets(parts_data.part_name);
		printf("\nNumber of parts => ");
		scanf("%d", &parts_data.quantity);
		printf("Cost per part => ");
		float cost;
		scanf("%f", &cost);
		parts_data.cost_each = cost;
		fwrite(&parts_data, sizeof(parts_data), 1, file_pointer);
		printf("Add more parts (Y/N)? => ");
	} while(getche() == 'Y');
	fclose(file_pointer);

	return 0;
}
Chủ đề liên quan
Tập Tin C++

Cùng chuyên mục

Xem nhiều hôm nay