⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.cpp

📁 本程序介绍了如何用C语言快速读写磁盘文件。
💻 CPP
字号:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

extern void fputc_fgetc_test();
extern void fgets_fputs_sscanf_test();
extern void fscanf_fprintf_test();
extern void fread_fwrite_test();
extern void fseek_ftell();
extern void file_encrypt();
extern void bin_file_copy_test();

#define  MAX_NUM_test_struct       6

typedef struct
{
	char name[10];
	char sex;
	int age; 
}test_struct;



void main()
{

//	fputc_fgetc_test();
//	fgets_fputs_sscanf_test();
//	fscanf_fprintf_test();
//	fread_fwrite_test();
//	fseek_ftell();
	file_encrypt();
//	bin_file_copy_test();


	return;
}


/**************************************************************************
	fseek(文件指针,位移量(字节数),起始点)
	eg: fseek(fp,100L,SEEK_SET) // SEEK_SET==0,文件头
	    fseek(fp,50L,SEEK_CUR)  // SEEK_CUR==1,文件当前位置
		fseek(fp,-10L,SEEK_END) // SEEK_END==2,文件尾
***************************************************************************/
void fseek_ftell()
{
	test_struct list_tmp[MAX_NUM_test_struct] = {0};
	test_struct list[MAX_NUM_test_struct]	=
	{ 
		{"litong",'m',30},{"wangma",'f',21},{"hongja",'m',18},
		{"xiaomi",'f',30},{"zhaxue",'m',21},{"wuping",'f',18}
	};
	FILE *fp;
	int i;
	int pointer_position;

	if((fp = fopen( "E:\\C++_sample\\File_operating\\File_testing.txt", "w+" ))== NULL)	
	{
		printf("cannot open the file\n");
		exit(0);
	}
	for( i = 0; i < MAX_NUM_test_struct; i++ )  // finish writing data to file
		if( fwrite(&list[i],sizeof(test_struct),1,fp) != 1 )
		{
			printf("file write error!\n");
			exit(0);
		}

	for( i = 0; i < MAX_NUM_test_struct; i += 2 )  
	{
		fseek( fp, i * sizeof(test_struct), SEEK_SET ); // set the location of pointer 
		if( fread(&list_tmp[i],sizeof(test_struct),1,fp) != 1 )
			printf("file read error!\n");
		else
			printf("%s\t\t%c\t\t%d\n",list_tmp[i].name,list_tmp[i].sex,list_tmp[i].age);
	}
	i -= 2;

	pointer_position = ftell(fp);
	printf("\nThere is %d bytes before the location of fp pointer.\n",pointer_position);
	pointer_position = sizeof(test_struct) * (i += 1);
	printf("\nIt is right that there is %d bytes before the location of fp pointer.\n\n",pointer_position);
	fclose(fp);

	return;
}

/*********************************************************************************
	fread(buffer,size,count,fp);  // size:number of bytes
	fwrite(buffer,size,count,fp); // count:number of size
	return the value of count.
*********************************************************************************/
void fread_fwrite_test()
{
	test_struct list_tmp[MAX_NUM_test_struct] = {0};
	test_struct list[MAX_NUM_test_struct]	= 
	{ 
		{"litong",'m',30},{"wangma",'f',21},{"hongja",'m',18},
		{"xiaomi",'f',30},{"zhaxue",'m',21},{"wuping",'f',18}
	};
	FILE *fp;
	int i,read_flag;

	if((fp = fopen( "E:\\C++_sample\\File_operating\\File_testing.txt", "w+" ))== NULL)	
	{
		printf("cannot open the file\n");
		exit(0);
	}
	for( i = 0; i < MAX_NUM_test_struct; i++ )
		if( fwrite(&list[i],sizeof(test_struct),1,fp) != 1 )
			printf("file write error!\n");

	rewind(fp);

#if 1
	for( i = 0; i < MAX_NUM_test_struct; i++ )
		if( fread(&list_tmp[i],sizeof(test_struct),1,fp) != 1 )
			printf("file read error!\n");
		else
			printf("%s\t%c\t%d\n",list_tmp[i].name,list_tmp[i].sex,list_tmp[i].age);

#else   
	while(1)
	{
		read_flag = fread(&list_tmp[0],sizeof(test_struct),1,fp);
		if( feof(fp) )
			break;

		if( read_flag != 1 )
			printf("file read error!\n");
		else 
			printf("%s\t\t%c\t\t%d\n",list_tmp[0].name,list_tmp[0].sex,list_tmp[0].age);
	}
#endif

	fclose(fp);

	return;

}


/********************************************************
	fprintf(文件指针,格式字符串,输出表列);
	fscanf(文件指针,格式字符串,输入表列);
*********************************************************/
void fscanf_fprintf_test()
{

	char str_1[10]="abc";
	char str_2[10]="def";
	int i=1000;
	FILE *fp;

	fp = fopen( "E:\\C++_sample\\File_operating\\File_testing.txt", "r+" );
	if(fp == NULL)
	{
		printf("cannot open the file\n");
		exit(0);
	}

	printf("\nsrc:\n%s,%s,%d\n",str_1,str_2,i);

	fprintf(fp,"%s\n%s\n%d",str_1,str_2,i);
	rewind(fp); // equal to fseek(fp,0L,SEEK_SET);
	fscanf(fp,"%s%s%d",str_1,str_2,&i); 

	printf("\ndes:\n%s,%s,%d\n",str_1,str_2,i);

	return;

}

/*************************************************************************************
	fgets(str_array,n,fp); //fetch n-1 characters from fp file and store in str_array
	fputs(str_array/str_const/str_p,fp);
**************************************************************************************/
void fgets_fputs_sscanf_test()
{
	char m_source[1025];
	char m_target[1025];
	FILE *fp;
	char str1[1025],str2[1025],str3[1025],str4[1025];

	fp = fopen( "E:\\C++_sample\\File_operating\\fgets_fputs_sscanf_testing.txt", "r" );//"rt"?
	if(fp == NULL)
	{
		printf("cannot open the file\n");
		exit(0);
	}
	fgets(  str1, 1024, fp );
	fgets(  str2, 1024, fp );
	sscanf( str2, "%s", m_source );
	fgets(  str3, 1024, fp );
	fgets(  str4, 1024, fp ); 
	sscanf( str4, "%s", m_target );
	fclose(fp);
	printf("\n%s\n%s\n",m_source,m_target);

	fp = fopen( "E:\\C++_sample\\File_operating\\copy_fgets_fputs_sscanf_testing.txt", "w" );//"rt"?
	fputs(str1,fp);
	fputs(str2,fp);
	fputs(str3,fp);
	fputs(str4,fp);
	fclose(fp);
	printf("Please check the file\n");

	return;
}

void fputc_fgetc_test()
{
	FILE *fp;
	char ch;

	fp = fopen( "E:\\C++_sample\\File_operating\\File_testing.txt", "w+" );//"a+"
	if(fp == NULL)
	{
		printf("cannot open the file\n");
		exit(0);
	}
	ch = getchar();
	while(ch != '#')
	{
		fputc(ch,fp);
		ch = getchar();
	}
	fclose(fp);

	fp = fopen( "E:\\C++_sample\\File_operating\\File_testing.txt", "r+" );//"w+"
	while(1)   
	{
		ch = fgetc(fp);
		if( feof(fp) )
			break;
		putchar(ch);
	}
	printf("\n");
	fclose(fp);


	return;
}

void file_encrypt()
{
	FILE *fp;
	int i,j;
	char str[1024];

	fp = fopen( "E:\\C++_sample\\File_operating\\File_testing.txt", "r+" );
	if(fp == NULL)
	{
		printf("cannot open the file\n");
		exit(0);
	}

	i = 0;
	while(1)
	{
		str[i] = fgetc(fp);
		if( feof(fp) )     //fp overflow,reach the end of file.
			break;
		str[i++] += 1;
	//	str[i++] -= 1;
	}

	fseek(fp,0L,SEEK_SET);
	for( j = 0; j < i; j++ )
		fputc(str[j],fp);

	fclose(fp);

	printf("please check the File_testing.txt file\n");
	return;
}

void bin_file_copy_test()  
{
	FILE *fp_in,*fp_out;
	unsigned char ch;

	if( (fp_in = fopen( "E:\\C++_sample\\File_operating\\bin_file_test.m4v", "rb" )) == NULL )
	{
		printf("cannot open the file bin_file_test.m4v\n");
		exit(0);
	}

	if( (fp_out = fopen( "E:\\C++_sample\\File_operating\\copy_bin_file.m4v", "wb" )) == NULL )
	{
		printf("cannot open the outfile\n");
		exit(0);
	}

	while(1)  
	{
		ch = fgetc(fp_in);
		if( feof(fp_in) )   //feof function returns "non_zero" when fp overflows in file,that is,
			break;          //if fp is located in the end of file and fp moves to next element 
		fputc( ch, fp_out );//again,then fp overflows.
	}

	fclose(fp_in);
	fclose(fp_out);

	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -