【发布时间】:2018-09-15 16:01:32
【问题描述】:
我正在编写一个可以读取和合并 3 个文件的 C 程序(程序尚未完成),但是,当我测试时,我意识到程序可以编译但控制台上的屏幕仍然是空白的!
感谢任何帮助,尤其是为什么它是空白的?
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("test");
//open three files for merging
FILE *fp1 = fopen("american0.txt","r");
FILE *fp2 = fopen("american1.txt","r");
FILE *fp3 = fopen("american2.txt","r");
printf("test");
//open file to store the result
FILE *fpm = fopen("words.txt", "w");
//creating an array to save the files data
char c;
char mergedFile[50];
//checking to make sure files are being read
if(fp1 == NULL && fp2 == NULL && fp3 == NULL && fpm == NULL)
{
printf("Could not open one or all of the files.\n");
printf("Exiting program!");
exit(0);
}
printf("test");
//initializing counter values
//inserting data from file into an array
while ((c = fgetc(fp1)) != EOF)
{
fputc(c, mergedFile);
}
while ((c = fgetc(fp2)) != EOF)
{
fputc(c, mergedFile);
}
while ((c = fgetc(fp3)) != EOF)
{
fputc(c, mergedFile);
}
printf("%s",mergedFile[0]);
printf("test");
return 0;
}
【问题讨论】:
-
fputc需要FILE*作为第二个参数。在将声明为char mergedFile[50];的mergedFile传递给那些函数调用时,您是如何地球 得到它甚至编译 的?这些不应该都使用fpm吗?据我所见,mergedFile在这段代码中根本毫无意义。删除它及其所有用法也将修复printf("%s",mergedFile[0]);,这本身没有意义,因为%s需要const char*,而您将char传递给printf调用。 -
好吧,我 compiled and ran it 确实打印了一些东西,不知道你到底出了什么问题。不要误会,因为这感觉像是一个愚蠢的问题,但你真的在编译后运行它吗?编译不会执行程序,它只会产生一个你可以执行的程序。
-
我觉得这是我正在做的一件小事和愚蠢的事情,但是,我编译了程序,所有的警告都打印出来了,当我运行它时使用“./a. out" 它不会打印出任何东西
-
我也只是把 printf("%s", mergeFile[0]);作为我意识到是错误的测试语句,但感谢您在 while 语句中指出合并文件的问题:)
-
我认为问题在于我在 while 循环中传递合并文件,谢谢您的帮助