龙空技术网

用FILE函数给文本文件排序

抖点料er 1146

前言:

而今兄弟们对“file函数的用法”大约比较珍视,大家都想要了解一些“file函数的用法”的相关内容。那么小编在网上网罗了一些关于“file函数的用法””的相关文章,希望兄弟们能喜欢,看官们快快来了解一下吧!

1.将一个随机数写到一个文本文件中

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>//将随机数写到文本文件中,并给随机数排序int main0601(){	srand((unsigned char)time(NULL));	FILE* p = fopen("F:/a.txt", "w");	if (p)	{		for (int i = 0; i < 100; i++)		{			int seq = rand() % 256;			char buf[100] = { 0 };			sprintf(buf, "%d\n", seq);//将证书转换为字符串 写到buf中			fputs(buf, p);//将buf写到p中		}		fclose(p);	}}

2.给这个文本文件中的随机数排序

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>  int main0602(){	FILE* p = fopen("F:/a.txt", "r");	int arr[100] = { 0 };	int index = 0;	while(!feof(p))	{		char buf[1024] = { 0 };		fgets(buf, sizeof(buf), p);		arr[index] = atoi(buf);		index++;	}	fclose(p);	Bubble_Sort(arr, 100);	p = fopen("F:/a.txt", "w");	for (int i = 0; i < 100; i++)	{		char buf[1024] = { 0 };			sprintf(buf, "%d\n", arr[i]);			fputs(buf, p);	}	fclose(p);	return 0;}

3.给这个任意大小的文本文件排序

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>//任意大小的文件排序int main0603(){	FILE* p = fopen("F:/a.txt", "r");	int arr[256] = { 0 };	while (!feof(p))	{		char buf[1024] = { 0 };		fgets(buf, sizeof(buf), p);		int a = atoi(buf);		arr[a]++;//统计相同数字出现的次数	}	close(p);	p = fopen("F:/a.txt", "w");	for (int i = 0; i < 256; i++)	{		for (int j = 0; i < arr[i]; j++)		{			char buf[100] = { 0 };			sprintf(buf, "%d\n", i);			fputs(buf, p);		}	}	fclose(p);	return 0; }

4.给文本文件中的内容解析出来并求值

如:20*5=

15/3=

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>//将文本中的数学表达式求值并保存到文件static int func1(int a, char b, int c){	switch (b)	{	case '+':		return a + c;	case '-':		return a - c;	case '*':		return a * c;	case '/':		return a / c;	}	return 0;}#define NUM 100int main0701(){	FILE* p = fopen("F:a.txt", "r");	//char arr[100][100] = { 0 };.	char* arr = calloc(NUM, sizeof(char));	char* temp = arr;//代表当前需要写入的字符位置	int index = 0;	while (1)	{		char buf[100] = {0};		fgets(buf, sizeof(buf), p);		if (feof(p))			break;		int a = 0;		char b = 0;		int c = 0;		sscanf(buf, "%d%c%d=", &a, &b, &c);		sprintf(arr[index],"%d%c%d=%d\n", a, b, c,func1(a,b,c));		arr = realloc(arr, NUM * (index + 2));		temp = arr + (NUM * (index + 1));//arr永远指向堆内存首地址,temp每次向后移动100字节		index++;	}	fclose(p);	p = fopen("a.txt", "w");	temp = arr;//让temp回到起始位置	for (int i = 0; i < index; i++)	{		fputs(arr[index], p);		temp += NUM;	}	fclose(p);	return 0;}

5.fprintf、和fscanf

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>int func1(int a, char b, int c){	switch (b)	{	case '+':		return a + c;	case '-':		return a - c;	case '*':		return a * c;	case '/':		return a / c;	}	return 0;} int main0801()//fscanf和fprintf{	FILE* p = fopen("F:/a.txt", "r");	FILE* p1 = fopen("F:/b.txt", "w");	while (1)	{		int a = 0;		char b = 0;		int c = 0;		fscanf(p, "%d%c%d=", &a, &b, &c);//从文件中读取内容		if (feof(p))			break;		fprintf(p1,"%d%c%d=%d\n", a, b, c,func1(a,b,c));//将输出结果放到p的文件中	}	fclose(p);	fclose(p1);	return 0;}int main0802(){	FILE* p = fopen("F:/a.txt", "r");	while (1)	{		char buf[100] = { 0 };		int age = 0;		//fscanf(p, "年龄=%d", &age);		fgets(buf, sizeof(buf), p);		if (feof(p))			break;		sscanf(buf, "年龄=%d", &age);		printf("%d\n", age);	}	close(p);	return 0;}//从一个文本文件中读取姓名和年龄  //文本文件内容 如:  姓名=黎明,年龄=45int main0803(){	FILE* p = fopen("F:/a.txt", "r");	while (1)	{		char  buf[1042] = { 0 };		fgets(buf, sizeof(buf), p);		if (feof(p))			break;		char* s = strtok(buf, ",");		char* name = strtok(buf, "=");		printf("%s\n", &name[1]);		s = strtok(NULL, ",");		//printf("%s\n", &s[5]);		printf("%d\n", atoi(&s[7]));	}	fclose(p);	return 0;}	

5.给文本文件中成员年龄第二大的人输出

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h>//从一个文本文件中读取姓名和年龄   并且给该成员排序,输出年龄第二大的人int main0901()//输出第二大的数字{	int a[10] = { 55,25,11,88,99,78,52,63,87,5 };	int max=0;//最大的数	int smax=0;//第二大的数	for (int i = 0; i < 10; i++)	{		if (a[i] > max)		{			smax = max;			max = a[i];		}		else if (a[i]<max && a[i]>smax)		{			smax = a[i];		}	}	printf("%d\n", smax);	return 0;}int main0902(){	FILE* p = fopen("f:/a.txt", "r");	int max = 0;	int smax = 0;	char max_name[100] = { 0 };	char smax_name[100] = { 0 };	while (1)	{		char buf[1024] = { 0 };		fgets(buf, sizeof(buf), p);		if (feof(p))			break;		char* s = strtok(buf, ",");		char* name = strtok(buf, "=");		s = strtok(NULL, ",");		if (atoi(&s[5] > max))		{			smax = max;			max = atoi(&s[5]);			strcpy(smax_name, max_name);			strcpy(max_name, &name[1]);		}		else if (atoi(&s[5] < max && atoi(&s[5])) > smax)		{			smax = atoi(&s[5]);			strcpy(smax_name, &name[1]);		}	}	fclose(p);	printf("%s\n", smax_name);	return 0;}

6.stat函数

#include<sys/stat.h>#include <unistd.h>#include<sys/types.h>int stat(const char* filename,struct stat*buf);

stat.at_size;//文件大小,单位:字节

函数的参数1代表文件名,参数2是struct stat结构。

函数说明:通过文件名filename获取文件信息,并保存在buf所指的结构体stat中。

返回值:执行成功则返回0,失败返回-1,错误代码存于errno。

错误代码:

ENOENT

参数file_name指定的文件不存在

ENOTDIR

路径中的目录存在但却非真正的目录

ELOOP

欲打开的文件有过多符号连接问题,上限为16符号连接

EFAULT

参数buf为无效指针,指向无法存在的内存空间

EACCESS

存取文件时被拒绝

ENOMEM

核心内存不足

ENAMETOOLONG

参数file_name的路径名称太长

	struct stat {    dev_t st_dev; //文件的设备编号    ino_t st_ino; //节点    mode_t st_mode; //文件的类型和存取的权限    nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1    uid_t st_uid; //用户ID    gid_t st_gid; //组ID    dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号    off_t st_size; //文件字节数(文件大小)    unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)    unsigned long st_blocks; //块数    time_t st_atime; //最后一次访问时间    time_t st_mtime; //最后一次修改时间  	time_t st_ctime; //最后一次改变时间(指属性)	};
#include<stdio.h>#include<sys/types.h>#include<sys/stat.h> int main0101(){	struct stat st = { 0 };	stat("F:/a.txt", &st);//调用完stat函数后,st里得到文件属性	int size = st.st_size;//得到文件大小	printf("%d\n", size);	return 0;}

7.fread和 fwrite函数

对文件格式化读写函数 fprintf 与 fscanf 而言,尽管它可以从磁盘文件中读写任何类型的文件,即读写的文件类型可以是文本文件、二进制文件,也可以是其他形式的文件。但是,对二进制文件的读写来说,考虑到文件的读写效率等原因,还是建议尽量使用 fread 和 fwrite 函数进行读写操作。

size_t fread(void *buf, size_t size, size_t count, FILE *fp);

size_t fwrite(const void * buf, size_t size, size_t count, FILE *fp);

参数1:buf内存地址;参数2:每次读取字节数;

参数3:读取次数;参数4:fopen返回的文件指针

即 fread 函数从文件 p 中读出“size*count”个字节保存到 buf 中,而 fwrite 把 buf 中的“size*count”个字节写到文件 fp 中。

fgets fputs fprintf, fscanf这些函数都是针对文本文件读写的,不能对一个二进制文件进行读写。

除了文本文件之外的文件都是二进制,比如图像,可执行程序,音乐,这些均为二进制文件。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>//fwrite   将一个整数写进二进制文件int main0201(){	FILE* p=fopen("F:/a.dat", "w");	int a = 546565321;	fwrite(&a, 1, sizeof(int), p);//往文件p里写4字节内容,内容是100//	fwrite(&a, sizeof(int), 1, p);//写1个整数  同上	fclose(p);	return 0;}int main0202(){	FILE* p = fopen("f:/a.dat", "r");	while (1)	{		int a = 0;		fread(&a, 1, sizeof(int), p);		if (feof(p))			break;		printf("%d\n", a);	}	fclose(p);	return 0;}

8.fopen的a、b模式

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>//fopen_a追加模式int main0301(){	FILE* p = fopen("f:/a.txt", "a");	fputs("helloworld\n", p);	fclose(p);	return 0;}//fopen_rb+  读写打开一个二进制文件,允许读写数据,文件必须存在/*windows所有的文本文件均以\r\n为结尾,而不是\n结尾如果读文件时参数是"r",那么系统会将\n前的\n自动隐藏若在参数"r后"添加"b",即"rb"那么\r会显示出来*/int main0302(int argc,char**argv){	FILE* p = fopen(argv[1], "wb");	char a[100] = { "hello\nworld "};	fputs(a, p);	close(p);	return 0;}

9.二进制文件的拷贝

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<sys/stat.h>#include<string.h>//二进制文件的拷贝#define NUM 1024*64//64kint main0401(int argc,char** argv){	if (argc < 3)		return 0;	FILE* p = fopen(argv[1], "rb");	if (p == NULL)		return 0;	FILE* p1 = fopen(argv[2], "wb");	if (p1 == NULL)		return 0;	unsigned int index = 0;//统计循环多少次	struct stat st = { 0 };	stat(argv[1], &st);	int size = st.st_size;//得到文件大小	if (size >= NUM)//如果文件小于64k 就开辟64k 若大于则根据实际大小开辟		size = NUM;	char* buf = malloc(size);//根据文件大小,动态分配内存	while (!feof(p))	{		//char a[1024] = {0};		int res=fread(buf, 1, size, p);		fwrite(buf, 1, res, p1);		index++;	}	free(buf);	fclose(p);	fclose(p1);	printf("%u\n", index); //输出循环次数	return 0;}

10.fseek和ftell函数

int fseek(FILE *stream, long int offset, int whence)

· stream 这是指向 FILE 对象的指针,该 FILE 对象标识了流。

· offset 这是相对 whence 的偏移量,以字节为单位。

· whence 这是表示开始添加偏移 offset 的位置。它一般指定为下列常量之一:

SEEK_SET

文件的开头

SEEK_CUR

文件指针的当前位置

SEEK_END

文件的末尾

返回值:如果成功,则该函数返回零,否则返回非零值。

注意:超出文件末尾,返回值为0,往回偏移超出首位置返回值还是0。

ftell() 函数用来获取文件读写指针的当前位置。

其原型为:long ftell(FILE * stream);

返回值:成功则返回当前的读写位置,失败返回-1。

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<sys/stat.h>#include<string.h>/*fseek函数  任意改变文件指针的位置SEEK_SET 开始SEEK_CUR 当前SEEK_END 末尾*/int main0501(){	FILE* p = fopen("f:/a.dat", "w");	char a[10] = { 1,2,3,4,5,6,7,8,9,10 };	fwrite(a, 1, sizeof(a), p);	fclose(p);	return 0;}//ftell函数,功能是告诉当前指针的位置  通过ftell得到文件大小int main0502(){	FILE* p = fopen("f:/a.dat", "r");	//通过ftell得到文件大小	fseek(p, 0, SEEK_END);	int size = ftell(p);	printf("size=%d\n", size);//size即文件大小	fseek(p, 0, SEEK_SET);//再将指针放到初始位置	fseek(p, 2, SEEK_SET);//将文件指针从开始位移2个指针	//while (!feof(p))	for(int i=0;i<2;i++)	{		char a[2];		fread(&a, 1, sizeof(a), p);		//fseek(p, -2, SEEK_CUR);//从当前位置退回位移2个字节		//fseek(p, 0, SEEK_SET);//回到文件开始		//fseek(p, 0, SEEK_END);//到达文件末尾		printf("%d,%d\n", a[0],a[1]);		int loc = ftell(p);//获取当前指针位置		printf("loc=%d\n", loc);	}	fclose(p);	return 0;}//利用fseek生成大文件int main0503(){	FILE* p = fopen("f:/a.dat", "w");	for (int i = 0; i < 100; i++)	{		char a = 0;		fwrite(&a, 1, sizeof(a), p);	}	fclose(p);	return 0;}//利用fseek快速生成大文件int main0504(){	FILE* p = fopen("f:/a.dat", "w");	fseek(p, 1000, SEEK_SET);	char a = 0;	fwrite(&a, 1, sizeof(a), p);	fclose(p);	return 0;}//通过ftell得到文件大小int main(int argc, char** args){	if (argc < 2)		return 0;	if (p == NULL)		return 0;	FILE* p = fopen("f:/a.dat", "r");	int res=fseek(p, 0, SEEK_END);	printf("%d\n", res);	int size = ftell(p);	printf("size=%d\n", size);	fclose(p);	return 0;}

标签: #file函数的用法 #c中fread函数的建议使用方法