Reading data from a text file

This is a discussion on Reading data from a text file within the C++ Programming forums, part of the General Programming Boards category; I am using this struct to store data from a text file Code: struct sItem { short unsigned int Number; ...

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Reading data from a text file

    I am using this struct to store data from a text file
    Code:
    struct sItem
    {
        short unsigned int  Number;
        sDate Date;
        std::string Name;
        std::string Memo;
        std::string MainCatagory;
        std::string SubCatagory;
        long double Amount;
        std::string Type;
    };
    the file uses tags to determine what data member each line belongs to like this : (CODE_TAG)(DATA)
    Code:
    <STMTTRN>
    <TRNTYPE>DTSF
    <DTPOSTED>20080331
    <TRNAMT>1211.65
    <NAME>SUBDT-TR
    <MEMO>8105T
    </STMTTRN>
    Each line in the file is saved to a std::string and the tag is extracted to determine which data member the line should be saved to.
    Code:
    do {
        read line into std::string temp;
        extract first part of line into std::string Code_Tag;
        extract the rest of the line into std::string Data;
        using Code_Tag, determine what Data is and save it to the approperiate sItem member;
    } while (NOT EOF)
    I'm just not sure of the best method to do this. I could use a bunch of if...then statements like this
    Code:
    if (Code_Tag == "TRNTYPE") Item.Type = data;
    if (Code_Tag == "NAME") Item.Name = data;
    ...
    But I don't like the way that looks, and it seems like there would be too much over head with all the 'if...then' tests.

    I could use a switch...case statement but VC Toolkit 2003 will not let me switch a std::string.

    I have also tried enumerating the code tags like this
    Code:
    typedef enum CODE_TAGS
    {
        TAG_START      = 0xff01,
        TAG_TYPE       = 0xff02,
        TAG_DATE       = 0xff03,
        TAG_AMOUNT     = 0xff04,
        TAG_NAME       = 0xff05,
        TAG_MEMO       = 0xff06,
        TAG_ITEMNUM    = 0xff07,
        TAG_END        = 0xff08
    };
    and just switching that, but then I have to convert each line from the std::string to the enum value. This seems to be just as much of a pain as the first idea.

    Any thoughts / ideas on what the best way would be to go about doing this?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    A switch must have a constant condition - something that can be evaluated at compile time. Therefore, a std::string cannot be switched.
    One approach to this is to use a map. The std::string as key, and a pointer to your data as value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I think the if/else if statements are fine.

    You could also make your struct store a map of string (tag value) to string (data value) for the string components. Then just add the entry to the map directly (the map would do the equivalent of the if/else but faster since it would be sorted). It gets a little more complicated since you are also storing shorts, doubles, and dates, though.

    BTW, I wouldn't use EOF to control your loop. I would use a while loop instead of a do while and then use while (read line into std::string temp).

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by Daved View Post
    It gets a little more complicated since you are also storing shorts, doubles, and dates, though.
    Although not impossible and quite easy with polymorphism and generic coding. I would find it an interesting challenge.
    If you feel up to it, that is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    The map idea is interesting. I have not delt a whole lot with maps so I may have to look into that. May be worth the challenge.

    I am not actually using EOF, the last line of the text file will have a tag that notes the end of file. That is what I am using to break out of the do...while loop. Although, I think that eventually I will have to abandon that idea and go to a more traditional EOF control
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Although not impossible and quite easy with polymorphism and generic coding.
    I wouldn't use polymorphism. I'd use separate maps or boost::variant.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Well, I guess our ways differ. Both certainly are possible, but as far as I see, using the polymorphic approach allows for more generic code.
    No need to keep adding different maps.
    Hmmm. Boost::variant, though... Isn't that just to store all different types inside one map and then try to query and guess the correct type and delegate it correctly? Somehow, I don't find that a very promising solution, unless you have a better way that I can't think of right now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> No need to keep adding different maps.
    No need, but it would be a much simpler solution that is preferred if the added complexity didn't buy you anything.

    >> Isn't that just to store all different types inside one map and then try to query and guess the
    >> correct type and delegate it correctly?
    No guessing required. You'd know the correct type by the key name.

    Polymorphism (and probably boost::variant, too) is much too complex of a solution for such a simple task.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by Daved View Post
    Polymorphism (and probably boost::variant, too) is much too complex of a solution for such a simple task.
    Unless it turns into something bigger, I agree.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. 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