如何在不使用任何内置函数的情况下将文件内容附加到另一个文件的末尾。 [英] How to append the contents of a file at the end of another file without using any built-in functions.

查看:44
本文介绍了如何在不使用任何内置函数的情况下将文件内容附加到另一个文件的末尾。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入第一个和第二个文件的名称后,当我输入合并文件的名称并按Enter键时,会弹出一个错误,指出错误已发生:没有这样的文件或目录。那可能是什么问题。



我尝试过的事情:



 / * C在另一个
* /
结尾附加文件内容的程序#include< stdio.h>
#include< stdlib.h>
main()
{
FILE * fsring1,* fsring2,* ftemp;
char ch,file1 [20],file2 [20],file3 [20];
printf(输入第一个文件的名称);
得到(file1);
printf(输入第二个文件的名称);
得到(file2);
printf(输入存储合并文件的名称);
得到(file3);
fsring1 = fopen(file1,r);
fsring2 = fopen(file2,r);
if(fsring1 == NULL || fsring2 == NULL)
{
perror(出错);
printf(按任意键退出...... \ n);
退出(EXIT_FAILURE);
}
ftemp = fopen(file3,w);
if(ftemp == NULL)
{
perror(Error has occurred);
printf(按任意键退出...... \ n);
退出(EXIT_FAILURE);
}
while((ch = fgetc(fsring1))!= EOF)
fputc(ch,ftemp);
while((ch = fgetc(fsring2))!= EOF)
fputc(ch,ftemp);
printf(两个文件合并%s成功。\ n,file3);
fclose(fsring1);
fclose(fsring2);
fclose(ftemp);
返回0;
}

解决方案

最明显的错误是你的输出文件没有创建,因为你没有提供有效的文件路径或你的app无法在此处创建文件。



提示输出文件使用w +标志。

< pre lang =c ++> fopen(file3, w +); // 创建和编写



获取错误消息并使用调试器解决这个问题。


After entering the name of first and second file when I enter the name of merged file and press enter it pops up an error saying Error has occured: No such file or directory. So what could be the issue.

What I have tried:

/* C Program to Append the Content of File at the end of Another
*/
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fsring1, *fsring2, *ftemp;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first file ");
gets(file1);
printf("Enter name of second file ");
gets(file2);
printf("Enter name to store merged file ");
gets(file3);
fsring1 = fopen(file1, "r");
fsring2 = fopen(file2, "r");
if (fsring1 == NULL || fsring2 == NULL)
{
perror("Error has occured");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
ftemp = fopen(file3, "w");
if (ftemp == NULL)
{
perror("Error has occures");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(fsring1)) != EOF)
fputc(ch, ftemp);
while ((ch = fgetc(fsring2) ) != EOF)
fputc(ch, ftemp);
printf("Two files merged %s successfully.\n", file3);
fclose(fsring1);
fclose(fsring2);
fclose(ftemp);
return 0;
}

解决方案

The most obvious error is that your output file isnt created because you havent provided a valid file path or your app has not the rights to create a file at this place.

Tip use the flag "w+" for the output file.

fopen(file3, "w+");//create and write


Fetch the error message and use the debugger for solving that issue.


这篇关于如何在不使用任何内置函数的情况下将文件内容附加到另一个文件的末尾。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆