-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge.c
73 lines (55 loc) · 1.84 KB
/
Merge.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
void mergeTxtFiles(const char *folderPath, const char *outputFileName) {
char searchPath[MAX_PATH];
snprintf(searchPath, sizeof(searchPath), "%s\\*.txt", folderPath);
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(searchPath, &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
printf("找不到txt文件。\n");
return;
}
char outputFilePath[MAX_PATH];
snprintf(outputFilePath, sizeof(outputFilePath), "%s\\%s.txt", folderPath, outputFileName);
FILE *outputFile = fopen(outputFilePath, "w");
if (outputFile == NULL) {
printf("无法创建合并文件。\n");
FindClose(hFind);
return;
}
do {
char filePath[MAX_PATH];
snprintf(filePath, sizeof(filePath), "%s\\%s", folderPath, findFileData.cFileName);
FILE *inputFile = fopen(filePath, "r");
if (inputFile == NULL) {
printf("无法打开文件 %s,跳过该文件。\n", filePath);
continue;
}
char c;
while ((c = fgetc(inputFile)) != EOF) {
fputc(c, outputFile);
}
fclose(inputFile);
} while (FindNextFile(hFind, &findFileData) != 0);
fclose(outputFile);
FindClose(hFind);
printf("文件合并完成,合并文件保存在 %s \n", outputFilePath);
}
int main() {
char folderPath[MAX_PATH];
char outputFileName[100];
printf("文件夹路径:");
scanf("%s", folderPath);
printf("合并后的文件名称:");
scanf("%s", outputFileName);
printf("正在合并...\n");
mergeTxtFiles(folderPath, outputFileName);
return 0;
}
/*
目前遇到的bug:
1.文件夹的路径如果包含空格则输出文件的路径并不在原路径
2.要合并的txt文件如果以数字开头则程序会一直执行下去
*/