gpt4 book ai didi

c - 逐行添加

转载 作者:太空宇宙 更新时间:2023-11-04 03:25:52 24 4
gpt4 key购买 nike

我目前必须添加并使其按这样的顺序进行

content of file 1
content of file 2
content of file 1
content of file 2

它目前只将一个文本文件的内容写入输出文件,我看不出我哪里出错了,所以非常感谢任何帮助。

**** 编辑,嗨做了一些挖掘,发现这个确切的问题已经得到回答,不想惹上 mods 的麻烦,也无法删除,谢谢大家

最佳答案

您没有存储 fopen() 返回的流指针,也没有交错 file1 和 file2 的行。以下是解决这些问题的方法:

    ...
// file 3
printf("Please enter the name of the output file : ");
if (scanf("%s", file3) != 1) {
printf("input error\n");
exit(1);
}
FILE *OpenFile = fopen(file1, "r");
FILE *OpenFile2 = fopen(file2, "r");
FILE *OpenFile3 = fopen(file3, "w");

if (OpenFile == NULL || OpenFile2 == NULL || OpenFile3 == NULL) {
perror("Error opening files");
printf("Press any key to exit!\n");
exit(1);
}

int c1 = 0, c2 = 0;
while (c1 != EOF || c2 != EOF) {
if (c1 != EOF) {
/* copy a line from file1 */
while ((c1 = fgetc(OpenFile)) != EOF && c1 != '\n') {
fputc(c1, OpenFile3);
}
fputc('\n', OpenFile3);
}
if (c2 != EOF) {
/* copy a line from file2 */
while ((c2 = fgetc(OpenFile)) != EOF && c2 != '\n') {
fputc(c2, OpenFile3);
}
fputc('\n', OpenFile3);
}
}

printf("The two files were successfully merged into %s\n", file3);

fclose(OpenFile);
fclose(OpenFile2);
fclose(OpenFile3);
return 0;

关于c - 逐行添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41108800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com