how quick using binary read file ?

This is a discussion on how quick using binary read file ? within the C Programming forums, part of the General Programming Boards category; Code: fopen("a.txt","r") fopen("a.txt","rb"); "r" and "rb" , which is more quick? why? if I use "rb", must I use fread ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    how quick using binary read file ?

    Code:
    fopen("a.txt","r")
    fopen("a.txt","rb");
    "r" and "rb" , which is more quick? why?

    if I use "rb", must I use fread function?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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 02:02 AM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Adak View Post
    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?
    In same conditions ( same HD same CPU and etc) , how much faster?
    almost 4% than text file mode ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,582
    Quote Originally Posted by zcrself
    In same conditions ( same HD same CPU and etc) , how much faster?
    almost 4% than text file mode ?
    If it matters, measure.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read a file of commands and execute them
    By surlyTomato in forum C Programming
    Replies: 5
    Last Post: 08-20-2009, 11:32 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Binary file is read with bytes missing
    By thomas41546 in forum C# Programming
    Replies: 1
    Last Post: 09-03-2006, 04:15 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21