0

I want to read a video file and save as binary and write as a video file again. I tested with 180MB video. I used fread function and It occur segmentation fault because array size is small for video.

those are my questions:

  1. I use 160*1024 bytes char array. What is the maximum size of char array? How I can solve this problem?

  2. this program need to work as:

    • read 128 bytes of video -> Encrypt -> write 128 byte
    • read next 128 bytes -> Encrypt -> write to the next.

I can't upload my code because of security rule of company. Any tip would be appreciated.

unwind
  • 391,730
  • 64
  • 469
  • 606
YeonJu Lee
  • 33
  • 5

1 Answers1

1

first use fseek() with SEEK_END, then use ftell() to determine the file size, after that allocate the needed memory with malloc() and write the data to that memory.

If I understand you correctly you don't need to allocate so much memory, but only 128 Bytes.

char buf[128];
while(/* condition */)
{
  ret = fread(buf, sizeof buf, 1, fp_in);
  encrypt(buf);
  ret = fwrite(buf, sizeof buf, 1, fp_out);
}
unwind
  • 391,730
  • 64
  • 469
  • 606
robin.koch
  • 1,223
  • 1
  • 13
  • 20