"r" and "rb" , which is more quick? why?Code:fopen("a.txt","r") fopen("a.txt","rb");
if I use "rb", must I use fread function?
"r" and "rb" , which is more quick? why?Code:fopen("a.txt","r") fopen("a.txt","rb");
if I use "rb", must I use fread function?
Yes, binary file mode is quicker, and you lose no clarity of the code, by using it. Recommended for speed critical code. How much faster depends on your everything from your data speed, to your HD, to your cpu and RAM and etc. If I had to guess, I'd say 4% faster than text file mode, depending on the size of the file, also.
Binary files have no translation involved with them being read. "What you got is what you get".
Text files have some work that must be done before you can work with them.
Well, that's a hell of a question! What's wrong with fread(), anyway?
Last edited by Adak; 01-12-2010 at 03:02 AM.
If it matters, measure.Originally Posted by zcrself
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It depends on how many newlines, etc., must be translated. I would guess 4% faster, but run your own tests. Short files, long files, files with lots of newlines, files with very few. You must test to be sure.
But bear in mind that file I/O is vastly slower than reading from memory, so your average processor isn't going to be over troubled by removing a few newlines from a block of memory (compared to the time it takes the disk to rotate the next sector under the read head).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.