It works fine for regular formatted files such as WORD-documents and text files, but with movies it stops after about 705 bytes, although the file is 42MB. I think it's because there is a EOF character in the middle of the file which breaks up the while loop. Does anyone know how to solve this?
~G
EOF is not stored with the file but is return by the operating system when its encountered.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&id=1043284351
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0
I've tried adjusting the modus for file reading/writing, but it did not solve the problem: the file copy is still stopped after 705 bytes.
Then you didn't fix the problem properly :icon_wink:
If I understand correctly, the difference in speed between fread/fwrite and getc/putc is the same?
You understand incorrectly. getc()/putc() is slower because it reads one byte at a time. fread()/fwrite() can read multiple bytes so it's much faster.
But isn't the getc opening the file each time it gets a character and then reopens the stream, in contrary to fread that would only need to access it one time? I think I'il try the fread/fwrite method in this case.
No, if getc() opened the stream every time you'd only be able to read the first byte of the file.
Assuming I find a solution to the EOF problem (probably using fseek, suggested by DeanM),...
Worthless and makes your code too complex. Just read a block, write a block, read a block, write a block... Keep it up until fread() returns EOF.
... will fread work correctly with really large files (e.g. 3GB +), as it would allocate an absurd amount of virtual memory if it does it all at once. Would it be better to do read/write every 50MB or so? Or should I depend it on the virtual memory available (and if so, how do I measure it with standard C functions)?
Read the file in small enough blocks that
1) it doesn't overload memory
2) you don't need dynamic (virtual) memory
When programming this task, read short files (100-200 bytes) and set your read block to 50 bytes. That way you can follow what's going on using the debugger without going crazy. Once it seems to be working, increase your buffer and file sizes.
Edit: gerard, if EOF is a standard value returned if the file-read has ended, why does it stop after 705 bytes?
You did something wrong. :icon_confused:
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Question Answered as of 1 Year Ago by
nezachem,
WaltP,
gerard4143
and 1 other