0

I have a video file and want to read it and show the result in anathor file.

FILE *fp1,*fp2;

fp1=fopen("FOOTBALL_352x288_30_orig_01.yuv","rb");   
fp2=fopen("FOOTBALL_352x288_30_copy_01.yuv","wb");

while (feof(fp1))
{
  fread(1,sizeof(int),fp1);
  fwrite(fp1,sizeof(int),fp2);
}

fclose(fp1);
fclose(fp2);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • this program doesnt work, any suggestions`? – Skander Ferjani Feb 07 '19 at 12:04
  • 1
    what is your issue? can you please add more context? – mast3rd3mon Feb 07 '19 at 12:05
  • 4
    You have `while (feof(...`, but probably meant `while (!feof...`. But see also [this question](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Karsten Koop Feb 07 '19 at 12:06
  • Raw YcbCr is normally in IYUV format which means that 1 frame is equal to `W*H*3/2` bytes. See eg a simple ycbcr [viewer](https://github.com/figgis/yuv-viewer) I wrote long time ago – Fredrik Pihl Feb 07 '19 at 12:08
  • 2
    What question are you asking? Use the value returned by `fread` to drive the loop and pass that to `fwrite`. Doesn't work? Doesn't even compile (not enough arguments). – Weather Vane Feb 07 '19 at 12:11
  • can you please try to correct my code, cuz i'm a beginner, what should i write in `fread` and `fwrite`. – Skander Ferjani Feb 07 '19 at 12:18
  • 2
    There is documentation and an example use of `fread` [here](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fread?view=vs-2017), although you can ignore the use of `fopen_s`. – Weather Vane Feb 07 '19 at 12:20
  • @mast3rd3mon my goal is to read a video file and try to copy on another file and to check if the code worked well, i open the copied file with YUV program, if it shows the same video, means everything works well. the issue is im not able to read and write the files correctly. – Skander Ferjani Feb 07 '19 at 12:22
  • Your question is not video related. Your goal seems simply to copy a file. Please confirm/elaborate. You can [edit] your question. and _doesn't work_ is not a problem description? Did it compile or not? If not, include at least some error messages. If it compiled, did it run? What happened? Are there any error messages? Etc. – Jabberwocky Feb 07 '19 at 12:26
  • Other notes: 1. `fopen` requires 4 parameters, you only provide 3. 2. You need to check the return value of `fopen`, if it is `NULL`, the file could not be opened and then it is pointeless to proceed. 3.did you include `stdio.h`?. 4. Did you read the documenation of `fread`? – Jabberwocky Feb 07 '19 at 12:29
  • Karsten spotted the typo. Voting to close this as simple typo. – Lundin Feb 07 '19 at 12:59
  • @Jabberwocky fopen requires 2 parameters, the OP provides those 2. You meant fwrite. – Lundin Feb 07 '19 at 13:00
  • @Lundin yes of course, thanks for pointing this out. – Jabberwocky Feb 07 '19 at 13:06

3 Answers3

3

You more or less want something like this:

#include <stdlib.h>
#include <stdio.h>

#define BUFFERSIZE 0x8000 // 32k buffer (adapt at will)

int main()
{
  FILE *fp1 = fopen("FOOTBALL_352x288_30_orig_01.yuv", "rb");

  if (fp1 == NULL)
  {
    // display error message to be written
    exit(1);
  }
  FILE *fp2 = fopen("FOOTBALL_352x288_30_copy_01.yuv", "wb");
  if (fp2 == NULL)
  {
    // display error message to be written
    exit(1);
  }    

  for (;;)   // loop for ever
  {
    char buffer[BUFFERSIZE];
    size_t bytesread = fread(buffer, 1, sizeof buffer, fp1);

    // bytesread contains the number of bytes actually read
    if (bytesread == 0)
    {
      // no bytes read => end of file
      break;
    }

    fwrite(buffer, bytesread, 1, fp2);
  }

  fclose(fp1);
  fclose(fp2);
}

Disclaimer: this is untested code, but you should get the idea.

There is still room for further improvement, especially actual read errors (that happen rarely) other than end of file are not handled correctly.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • thank you for your response, the program compile and generate any errors, but when i try to open the copied with the YUV Player software, an error window shows "Error Opening file" – Skander Ferjani Feb 07 '19 at 15:01
  • @SkanderFerjani as I wrote, this is untested code. Exchange parameters 2 and 3 in `fread` and it will work. I corrected the answer. – Jabberwocky Feb 07 '19 at 15:13
0

this is my solution, i tested and it seems working, what do you think guys

#include <stdio.h>
#include <math.h>
#include <stdint.h>


 
int main(void) {
    FILE *fp1, *fp2;
 
 fp1= fopen("FOOTBALL_352x288_30_orig_01.yuv","rb");
 
 fp2= fopen("FOOTBALL_352x288_30_copy_02.yuv","wb");
   
 
 
int buffer;
while(!feof(fp1))
 {
  fread((void *)&buffer, sizeof(buffer),1,fp1);
  fwrite((void *)&buffer, sizeof(buffer),1,fp2);
  }
  

    fclose(fp1);
    fclose(fp2);
 
 return 0;
}
-1

You want to read while !feof(fp1).

fread reads to a buffer... where is your buffer? fwrite writes the content from that buffer.

And surely you don't want to read sizeof(int), use a datatype with a specified size, like 8 bits in uint8_t.

uint8_t buffer;

Then you can do

fread((void *)&buffer, sizeof(uint8_t), 1, fp1);
fwrite((void *)&buffer, sizeof(uint8_t), 1, fp2);

Of course you should add some error handling as well...

Also this is very slow, as you read byte by byte :) But this is how it basically works.

andi8086
  • 394
  • 2
  • 9
  • 1
    *You want to read while `!feof(fp1)`.* [And that's wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – Andrew Henle Feb 07 '19 at 13:02
  • There is a lot of theory if usage of feof is good or bad... but that is not the point of the question here. The question is about usage of fread and fwrite. Of course he can read as long as feof (fp1) is not true. A lot of things can happen but that is not the point of the answer nor the question. – andi8086 Feb 07 '19 at 13:16
  • People remember... first get things working, then make them beautiful. – andi8086 Feb 07 '19 at 13:19
  • And *while `!feof(fp1)`* does not work. I'll leave it to you to learn why it doesn't work - you **need** to. – Andrew Henle Feb 07 '19 at 13:36
  • with `!feof(fp1)` works fine for me, if its wrong, what is the right answer? – Skander Ferjani Feb 07 '19 at 13:47
  • @AndrewHenle I already read that a year ago and despite I ignored it to answer this question simply since it is not the point here... feof works if you make simplifications to your environment. If the environment is predictable than you CAN predict that read will be successful if feof is false. That's why I don't agree with this nosensical discussion. Nobody has said anything about the environment. – andi8086 Feb 07 '19 at 14:43