Reputation: 21
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);
Upvotes: 0
Views: 4317
Reputation: 21
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;
}
Upvotes: 0
Reputation: 50902
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.
Upvotes: 3
Reputation: 413
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.
Upvotes: -1