Reading from a file

This is a discussion on Reading from a file within the C Programming forums, part of the General Programming Boards category; I have to optimize my reading from a text file. How can I do that ? What is the best ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Reading from a file

    I have to optimize my reading from a text file. How can I do that ? What is the best way to read from a file?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    Use fgets() in a while loop.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    yes... but will it run more faster. I hate to process the line to obtain what I want. Isn't it better to use fscanf() (i have to use it twice in order to read a char[] and an int ) ?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Why don't you try what Salem said, and when you have it working you can then do some profiling on the software while it runs - if, and only if, you are not satisfied with the speed, then you can take measures to increase the speed. No sense in optimising if the software will do everything you expect of it in the first place.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > I hate to process the line to obtain what I want. Isn't it better to use fscanf()
    1. Reading from disk is about a million times slower than reading from memory, so what you use to read from the file is pretty much irrelevant. They all suck on the performance front.

    2. Anything which attempts input AND conversion at the same time has simply horrible error recovery. What if your conversion fails on the int?

    Code:
    char buff[BUFSIZ];
    char mystring[BUFSIZ];
    int myint;
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
      if ( sscanf( buff, "%s %d", mystring, &myint ) == 2 ) {
        // success
      } else {
        // error in conversion
      }
    }
    fgets just reads a line from the file and stores it in a buffer - no mess, no fuss and perfectly predictable. If there's a problem or end of file, then it's dead easy to drop out of the loop.
    sscanf does the conversion, and if the number of conversions is as expected, then count it as success.

    Of course if you wanted to be super-safe, you would use strtol() say to convert the integer. It can detect integer overflow whereas sscanf() cannot.

    > yes... but will it run more faster.
    You've got to make it right before you can make it fast.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    thank you salem for explaining.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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