项目场景:
有一个名为rand_num.txt的txt数据文件,里面存了1000个随机的数,如下图:
需要用c语言读取,并写入四个新的txt文件。最后再排序归并到一个txt文件。本文只记录分割数据与归并数据的方法
问题描述:
这并不是我直接遇到的问题。事实上是有一位小姐姐在做老师给的一个大作业时使用了fscanf()和fprintf()函数组合做数据切割,用fread()和fwrite()函数做数据归并。
很不幸,方法都是对的,可是第一次写的时候都出现了数据乱码的错误。拿给我看的时候,我也被乱码的错误搞的一脸懵。我一开始就怀疑是代码错误引起的。然而事实上,人的大致思路是很少出错的,c语言标准库函数对编码格式对容忍度实际上也是很高的。
最后查出来,竟然都是一些细枝末节的语法,代码规范导致的一些无法被编译器即时捕捉的错误导致的。所以一个好的代码规范相当重要,既能让自己高效,也能少浪费他人的时间。
对于C语言这种面向过程的语言,我建议尽量把各个功能都分别交给独立的子函数完成。子函数的命名一定要是有实际意义的英文单词,接口参数也尽量如此命名。再就是主函数调用时一定把变量作为参数传递给子函数,千万不要直接带入变量的实际值,能减少很多类型引起的错误,方便重用代码和调试。
原因分析:
在使用fscanf()和fprintf()时,一定要注意这两个都是格式化的读写,fscanf()的参数要传入地址变量,尽量不要在循环读写的时候增加很多不必要的变量值和参数,也建议直接读写,减少中间变量数组的使用。解决方案:
本着提高代码可重用的目的,提供本人写好后复制粘贴可用的子函数实现。
divide子函数有五个参数,实现了把一个拥有1000个整数的txt文件均分成四个拥有250个整数的txt文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41int divide(const char *out1, const char *out2, const char *out3, const char *out4, const char *in) // out1-out4 are your output txt and in is your original txt { FILE *f_in, *f_out; int num, i; num = 1000; // set the number of the digits in your original txt. int buf[num]; int n; f_in = fopen(in, "r"); for(i=0; i<1000;i++) fscanf(f_in, "%d", &buf[i]); fclose(f_in); // open and close txt every time you use it f_out = fopen(out1, "w"); for(i=0; i<250; i++) fprintf(f_out, "%d ", buf[i]); fclose(f_out); // if you want to output digits with space, you should use "%d " instead of "%d" f_out = fopen(out2, "w"); for(i=250; i<500; i++) fprintf(f_out, "%d ", buf[i]); fclose(f_out); f_out = fopen(out3, "w"); for(i=500; i<750; i++) fprintf(f_out, "%d ", buf[i]); fclose(f_out); f_out = fopen(out4, "w"); for(i=750; i<1000; i++) fprintf(f_out, "%d ", buf[i]); fclose(f_out); return 1; // return 1 can help you know if you read and write successfully in your main function }
merge子函数也有五个参数,实现了把四个拥有250个整数的txt文件归并成一个txt文件的功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87int merge(const char *in1, const char *in2, const char *in3, const char *in4, const char *out) { FILE *f_in, *f_out; int buf[1000]; int n; f_out = fopen(out, "a"); if(!f_out) return 0; f_in = fopen(in1, "r"); if(!f_in) return 0; while((n=fread(buf,1,sizeof(buf),f_in))) // read by single byte { fwrite(buf,1,n,f_out); // write by single byte } char space[] = {' '}; fwrite(space,1,1,f_out); // add a space at the end fclose(f_in); fclose(f_out); // close f_out first f_out = fopen(out, "a"); if(!f_out) return 0; f_in = fopen(in2, "r"); if(!f_in) return 0; while((n=fread(buf,1,sizeof(buf),f_in))) // read by single byte { fwrite(buf,1,n,f_out); // write by single byte } fwrite(space,1,1,f_out); // add a space at the end fclose(f_in); fclose(f_out); // close f_out first f_out = fopen(out, "a"); if(!f_out) return 0; f_in = fopen(in3, "r"); if(!f_in) return 0; while((n=fread(buf,1,sizeof(buf),f_in))) // read by single byte { fwrite(buf,1,n,f_out); // write by single byte } fwrite(space,1,1,f_out); // add a space at the end fclose(f_in); fclose(f_out); // close f_out first f_out = fopen(out, "a"); // open file again with "a" if(!f_out) return 0; f_in = fopen(in4, "r"); if(!f_in) return 0; while((n=fread(buf,1,sizeof(buf),f_in))) { fwrite(buf,1,n,f_out); } fclose(f_in); fclose(f_out); return 1; // return 1 can help you know if you read and write successfully in your main function }
最后
以上就是高兴可乐最近收集整理的关于2021-07-31 记如何用C语言把txt数据文件切成四份,再归并项目场景:问题描述:原因分析:解决方案:的全部内容,更多相关2021-07-31内容请搜索靠谱客的其他文章。
发表评论 取消回复