Proper way to read in a file

This is a discussion on Proper way to read in a file within the C++ Programming forums, part of the General Programming Boards category; Problem: I need to read in a configuration file. The file is delimited with ';' and i need to parse ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    9

    Proper way to read in a file

    Problem:
    I need to read in a configuration file. The file is delimited with ';' and i need to parse out the values.

    example:
    r=value;m=value;u=value;

    What is the proper way to Read the file in. I am using ReadFile() after a get a handle to the file. The quandry I'm having is when i read the file in, i have to specify a buffer. This file usually won't be that big, but is there a way to specify the exact size of the file for the buffer at runtime or do i have to put an arbitrary value?

    Thanks for any help!

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The easy way would be something like
    Code:
    typedef std::map<std::string, std::string> valmap_t;
    typedef valmap_t::iterator valmap_it;
    
    bool parse(std::istream &is, valmap &m) {
        std::string name, value;
        while(std::getline(is,name,'=') && std::getline(is,value,';') m[name] = value;
        return is.eof();
    }
    ...
    std::ifstream is("datafile.txt");
    valmap_t values;
    if(parse(is,values)) {
        valmap_it it=values.find("v");
        if(it != values.end()) std::cout << "v is " << it->second();
        else std::cout << "v is undefined";
        std::cout << std::endl;
    } else {
        std::cerr << "parse error" << std::endl;
    }
    This could then be used with an ifstream from a file, or any other type of stream. The only problem you might have is that both name and value will include all whitespace, plus values will be case sensitive. Thus before you add a name to the map you may want to normalize it(trim leading and trailing whitespace and convert to lowercase).
    Last edited by grib; 04-28-2004 at 10:59 AM. Reason: Even more bugs, really should compile these things

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    this is how I would do it, given
    example:
    r=value;m=value;u=value;
    Code:
    ...
    
    char null;
    int value;
    ifstream infile("filename.ext");
    
    //loop
    infile>>null>>null>>value>>null;
    //do something with values
    //end loop
    
    ...
    or something liek this:
    Code:
    ...
    
    char line[128];
    ifstream infile("filename.ext");
    
    //loop
    infile.getline(line,128,';');
    //parse string
    //do something with values
    //end loop
    
    ...
    Last edited by major_small; 04-28-2004 at 11:02 AM. Reason: semicolons -.-
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 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. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 11:32 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

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