连接两个.y输入

时间:2015-04-23 14:48:16

标签: c

我有两个输入:video1.yvideo2.y。我想连接这两个文件来创建一个video.y文件?我在C中编写代码。它可能是基本的问题,但不能这样做!!两个输入都具有相同的高度和宽度。 代码:

int main()
{
   // Open two files to be merged
   FILE *fp1 = fopen("D:\\dump\\video1.y", "rb");
   FILE *fp2 = fopen("D:\\dump\\video2.y", "rb");

   // Open file to store the result
   FILE *fp3 = fopen("D:\\dump\\video.y", "wb");
   char c;  //Change char to int as per answer given by user3710044..
            //...Which is working !!
   if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
   {
         puts("Could not open files");
         exit(0);
   }

   // Copy contents of first file to video3.y
   while ((c = fgetc(fp1)) != EOF)
      fputc(c, fp3);

   // Copy contents of second file to video3.y
   while ((c = fgetc(fp2)) != EOF)
      fputc(c, fp3);

   printf("Merged video1.y and video2.y into video.y");

   fclose(fp1);
   fclose(fp2);
   fclose(fp3);
   return 0;
}

video1.y和video2.y是55 MB。我的输出是3 KB。我无法连接这两个输入

1 个答案:

答案 0 :(得分:2)

c变量的类型是char,它不能保存EOF的值和所有256个字节的值。

在您的计算机上发生char是签名类型,因此fgetc的投射返回结果和EOF的投射值实际上意味着找到了EOF在文件的末尾。问题是,如果文件包含0xFF字节,则这也被视为EOF

总之,请将c的类型更改为int