【问题标题】:C program complies but screen on console remains blackC程序符合但控制台上的屏幕仍然是黑色的
【发布时间】: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 循环中传递合并文件,谢谢您的帮助

标签: c linux debugging console


【解决方案1】:

错误 --> fputc 需要文件指针作为第二个参数而不是数组:int fputc ( int character, FILE * stream );

注意事项:

  1. 数组的大小应足以包含这些文件中的所有数据。
  2. 注意格式说明符及其在 char 数组中的要求。
  3. 如果数组的大小小于所有文件的总大小怎么办? - 错误处理
  4. 如果要读取/写入的文件位于其他目录中怎么办?

这是一个最小的修正版本:

     #include <stdio.h>
     #include <stdlib.h>
     #define MAX 1000     //ADDED NEW

     int main()
     {  
         //open three files for merging
         FILE *fp1 = fopen("american0.txt","r");
         FILE *fp2 = fopen("american1.txt","r");
         FILE *fp3 = fopen("american2.txt","r");

         //open file to store the result
         FILE *fpm = fopen("words.txt", "w");



         //creating an array to save the files data
         int c;                             
         int i=0;
         char mergedFile[MAX]={0};           //MODIFIED & INITIALIZED           

         //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);
         }
         //initializing counter values

         //inserting data from file into an array
         while (((c = fgetc(fp1)) != EOF)&&(i<MAX))  //MODIFIED
         {
             mergedFile[i++]=c;                      //MODIFIED
         }
         while (((c = fgetc(fp2)) != EOF)&&(i<MAX))  //MODIFIED
         {
             mergedFile[i++]=c;                      //MODIFIED
         }
         while (((c = fgetc(fp3)) != EOF)&&(i<MAX))  //MODIFIED
         {
             mergedFile[i++]=c;                      //MODIFIED
         }

         printf("%s",mergedFile);                    //MODIFIED

         return 0;

     }

【讨论】:

  • 使用int c; 比使用char c; 更好地处理来自fgetc() 的典型256 + 1 个不同结果。
  • 我知道你在复制 OP 的代码,但我认为检查文件打开成功的 if 语句应该是 || 而不是 &amp;&amp;,因为如果任何文件没有,以后会有问题'不打开,不只是如果所有的都没有打开。
  • @pstrjds OP 在printf 语句中的if 块中写道,如果所有文件都成功打开,则只能继续进行。
  • @user10334659 - 我知道这就是 OP 所写的(这就是我在评论前加上它的原因),但是由于您正在修复代码,我认为值得指出。如所写,只有当 all 的文件打开调用失败时才会如此。如果其中一个成功,代码将继续,这将在以后引起问题。
  • 非常感谢您的帮助。这消除了很多困惑!
猜你喜欢
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
相关资源
最近更新 更多