有趣生活

当前位置:首页>科技>文件排序怎么按末尾数字排序 用FILE函数给文本文件排序

文件排序怎么按末尾数字排序 用FILE函数给文本文件排序

发布时间:2026-07-21阅读(2)

导读1.将一个随机数写到一个文本文件中,现在小编就来说说关于文件排序怎么按末尾数字排序用FILE函数给文本文件排序?下面内容希望能帮助到你,我们来一起看看吧!文....

1.将一个随机数写到一个文本文件中,现在小编就来说说关于文件排序怎么按末尾数字排序 用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。

错误代码:

Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图