Thread: Question about IOStream and reading strings from files

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well if you're only writing this for that file, you could read in until a space and read in until a newline each time through the loop, after a few iterations, you'll end up with BallSpin in the string.

    Code:
    do {
       getline(fin,test, ' ');
       getline(fin,test, '\n');
       cout<<test<<endl;
       } while (test != name);
    Sent from my iPadŽ

  2. #17
    Registered User
    Join Date
    May 2005
    Posts
    23
    That is true but I am trying to start the read at ballspin not to get ballspin.
    I am may have been unclear to begin with because I am unclear on what is need to solve the problem and what the problem really is in the end. I thought it would be easy to find ballspin start reading the stop at ;. but I can see now it is not so easy.

    I am very appreciative for your help. I know with your help I will be able to solve this problem.
    Last edited by kingpinzs; 12-11-2005 at 05:54 PM.

  3. #18
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    There is no basic way to find something in a stream without having been reading from it already. But regardless, once you find the word ballspin, exit the loop, then start another loop that just keeps concatinating everything onto that same string until you get to the semicolon. That's about the easiest way to do it.
    Sent from my iPadŽ

  4. #19
    Registered User
    Join Date
    May 2005
    Posts
    23
    whould I do something like what is shown below?



    Code:
    do{
       getline(fin,test, ' ');
       getline(fin,test, '\n');
    
         cout<<test<<endl;
         }while (test !=name);
    
    test += blah;
    
    do{
       getline(fin,blah, ' ');
       getline(fin,blah, '\n');
    
    }while (blah !=";");

  5. #20
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    More like this:

    Code:
    do{
       getline(fin, test, ' ');
       getline(fin, test, '\n');
    
       }while (test !=name);
    
    
    do{
       getline(fin, blah, '\n');
       test += '\n' + blah;   // Don't forget getline() doesn't read in the delimeter
       }while (blah !=";");
    
    cout << test << endl;
    Last edited by SlyMaelstrom; 12-11-2005 at 07:05 PM.
    Sent from my iPadŽ

  6. #21
    Registered User
    Join Date
    May 2005
    Posts
    23
    only one issue if I cahnge it to BallExplode then it does not do any thing? is there something I have to put in so it will read it sence it is at the top?

    Edit:
    never mind I just have to be carefull with the spaces.

    Right now I am going to extract the data and if it works then I am all dont all thanks to you and your greate wisdom.

    Edit2:
    So how do I take string test and take out the individuel strings from it?
    Last edited by kingpinzs; 12-11-2005 at 07:31 PM.

  7. #22
    Registered User
    Join Date
    May 2005
    Posts
    23
    can any one else give me some ideas?

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I didn't follow the entire thread, so this may not be what you are looking for, but to take the individual strings out of an existing string, use a stringstream from <sstream>. It works just like an iostream or a file stream, so you can use getline or operator>> on it.

Popular pages Recent additions subscribe to a feed