c - 为什么我不能合并两个文件并将内容存储到 C 中的另一个文件
问题描述
void mergeFile(){
//allocate memory
char * firFile = (char*) malloc(MAX_SIZE);
char * secFile = (char*) malloc(MAX_SIZE);
char * conFile = (char*) malloc(MAX_SIZE);
char * buffer = (char*) malloc(MAX_SIZE);
char ch;
// get name of first file
printf("Enter name of first file: ");
__fpurge(stdin);
gets(buffer);
strcpy(firFile, FOLDER);
strcat(firFile, buffer);
//get name of second file
printf("Enter name of second file: ");
__fpurge(stdin);
gets(buffer);
strcpy(secFile, FOLDER);
strcat(secFile, buffer);
//get name of file will store
printf("Enter name of file which will store contents of two files : ");
__fpurge(stdin);
gets(buffer);
strcpy(conFile, FOLDER);
strcat(conFile, buffer);
//open 3 file with 'r' and 'w' mode
FILE * firPtr = fopen(firFile,"r");
FILE * secPtr = fopen(secFile, "r");
FILE * conPtr = fopen(conFile, "w");
//check 3 file NULL or not
if (firPtr == NULL) {
printf("Can not open %s file\n", firFile);
remove(conFile);
} else if (secPtr == NULL) {
printf("Can not open %s file\n", secFile);
remove(conFile);
} else if (conPtr == NULL){
printf("Can not open %s file\n",conFile);
}else{
// write all character in first file to file will store
// MAY NOT WORK
while ((ch = fgetc(firPtr)) != EOF)
fprintf(conPtr, "%c", ch);
// write all character in second file to file will store
// MAY NOT WORK
while ((ch = fgetc(secPtr)) != EOF)
fprintf(conPtr, "%c", ch);
printf("Two file were merged into %s file successfully\n!",conFile);
}
//clear all
free(buffer);
free(firFile);
free(secFile);
free(conFile);
fclose(firPtr);
fclose(secPtr);
fclose(conPtr);
}
我用来fget
从文件中获取字符并写入另一个文件,当我使用两个文件时我工作得很好,一个用于读取,一个用于存储,但是当我尝试将两个文件合并到另一个文件时,这段代码不起作用,不里面的东西包含文件。我在 Netbeans 8.2 中运行这段代码,你能从这段代码中给我错误吗,非常感谢!
解决方案
推荐阅读
- angular - 带有 Strapi 的 NgRx 数据(getByKey):无法解构“sourceError”Strapi 的属性“错误”
- python - 当我尝试编写代码以查找第一次出现的查询时,运行无限循环的 Jovian DSA 二进制搜索问题
- javascript - 检查一周是否在两周之间
- php - 不分大小写,检查char是否与字母条件相同 | PHP 和 HTML
- python - Python 根据文件数量对目录进行排序,Stanford Dogs Dataset Pytorch
- node.js - MacOS 上的 Nuxt JS SSL 服务器安装问题
- mysql - 为什么 MySQL 在使用带有通配符的 LIKE 运算符时忽略假名敏感?
- kotlin - 在 ktor 请求处理程序中启动协程
- c# - 使用asp.net的SQL语句中的动态条件
- reactjs - 与 TS 反应 - 使用 setInterval 和 useEffect() 如何发送请求,然后才开始间隔