Hi problems with str.substr and reading input files...

This is a discussion on Hi problems with str.substr and reading input files... within the C++ Programming forums, part of the General Programming Boards category; Greetings. I'm trying to write a program that reads a txt file one line at a time and then extracting ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Question Hi problems with str.substr and reading input files...

    Greetings.

    I'm trying to write a program that reads a txt file one line at a time and then extracting certain words and numbers from that line and storing them into strings and int (or floats), respectively.

    So, an example line from the file would be:

    field_1: string1 field_2: float1 field_3: int1 field_4: string2

    then with the program store them in the following manner:

    std::string p1 = string1
    std::float f1= float1
    std::int i1= int1
    std::string p2 = string2

    I think I know how to do it using a loop with getline(), string.find(), and str.substr().
    Here is the program:


    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <fstream>

    using namespace std;

    string line, p1, p2;
    int s1;
    float f1;

    ifstream myfile ("inputfile.txt");

    int main()
    {

    if (myfile.is_open())
    {
    while ( myfile.good() )
    {
    getline (myfile,line);
    cout << line <<endl;
    s1 = int(line.find("field_1 ")) + 8;
    s2 = int(line.find("field_2 ")) + 8;
    s3 = int(line.find("field_3 ")) + 8;
    s4 = int(line.find("field_4 ")) + 8;
    p1 = str.substr (s1);
    p2 = str.substr (s2);
    p3 = str.substr (s3);
    p4 = str.substr (s4);
    }
    }

    else cout << "Unable to open file";

    return 0;
    }
    On the program the "field_n" are replaced by the corresponding variable.
    And I know that I need to process the strings in order to convert them into ints or floats. I haven't included that part.

    So I'm making this post to see if anybody knows a better way to do what I'm trying to do, I'd appreciate any help. I'm also making this post because I think that using this I can achieve what I'm trying to do but I read on the other 3 topics regarding substrings that using substring on a loop is very inefficient, so again I'd take any suggestions. Lastly, my program doesn't recognize the str.substr command. I get the following error:

    'str' was not declared in this scope

    Thanks for any help provided.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Are there always four fields? Are they always in the same order? Can the strings have spaces?

    2. You don't have a string called str in your code listing. Did you mean line.substr?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Thank you very much for your response tabstop. Its always 6 fields. I suppose they'll always be in the same order. What strings do u mean can have spaces? The storage strings or the initial strings from the input file? To answer my own question, both strings may have spaces.

    Thanks for pointing out my error in your second statement . I'm going to try if it works. Will post results soon.

    Again, thanks a million. :P

    EDIT: Yea, works. Thanks. Still, if you can suggest a better way to do it I'm all ears because I really don't think the way I'm doing it is very good.
    Last edited by kocmohabt33; 01-19-2011 at 01:16 AM. Reason: Added results.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Having strings with spaces in them reduces your options considerably. At this point, you're pretty much stuck with searching through the string, looking for keywords (or at the least, looking for colons, with the part to the left being the key and the part to the right being the value).

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    K, thanks. I've been surfing a bit and I found that its a variant of the address book program and most of them are done with some sort of data container so I guess I'll stick to std::maps. Currently, I'm having a really difficult time understanding how iterators work. Do you think u can shed some light on them or know some good links with good explanations and examples?

    Thanks.

  6. #6
    Registered User rpbear's Avatar
    Join Date
    Nov 2009
    Posts
    18
    using stringstream will be easier:
    string sep,string1,string2;
    int int1;
    float float1;
    getline(file,str);
    istringstream ss(str);
    ss>>sep>>string1>>sep>>int1>>sep>>float1>>sep>>str ing2;
    then do other stuff....
    Last edited by rpbear; 01-20-2011 at 03:02 AM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Thanks for your reply. Can you elaborate a little bit? I'm having difficulties seeing how to apply that.. Remember that in each line there will be different strings and different number values that each have to go to a separate container. Thanks again.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    >> with a stringstream (just like >> on any stream) will not pick up a string with spaces in it, nor for that matter will it know to stop on a colon character. (The last is not really a big issue, as that actually makes it a little bit easier to check which strings are your keys (they're the ones with colons at the end), but the first can be a large pain to deal with.)

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Aight. The other day I read somewhere that you can store strings in arrays. -_-
    I wasn't aware of that. So now I'm doing redoing the whole program combining arrays and maps. It will be much easier. Thanks folks for the help, I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop reading input
    By c_newbie in forum C Programming
    Replies: 5
    Last Post: 12-08-2010, 09:09 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 12:40 PM
  3. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 02:48 AM
  4. Issues reading text files
    By ozzy34 in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2004, 09:15 AM
  5. function and input problems
    By meka in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2001, 12:56 PM

Tags for this Thread


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