Question about IOStream and reading strings from files

This is a discussion on Question about IOStream and reading strings from files within the C++ Programming forums, part of the General Programming Boards category; How do I start reading text file from a cretin string and then stop at another string?...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    23

    Question about IOStream and reading strings from files

    How do I start reading text file from a cretin string and then stop at another string?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    char foo[50];
       
    fgets(foo, 50, stdin);
       
    printf("Your string was %s", foo);
    If you want to find a string in a file, you could transverse through the file until you find what you're looking for. Just use a loop.
    Last edited by SlyMaelstrom; 12-11-2005 at 10:47 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    23
    I am not sure if that will work.
    1) I dont know the length of the text file
    2) I dont know were the string is located
    3)I am using fstream to open the file

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    fstream is C++

    and it's alot easier to find a string.

    Code:
    ifstream inFile;
    string foo;
    
    inFile.open("file.txt");
    
    do {
       getline(inFile, foo, '\n');
       } while (foo != "The string I want") // Or whatever you want. You might have to mess 
                   // with the delimeter to get the string you want.
    I don't know if C has a way to get a string up to a delimeter other than reading one character at a time. THere might be, but I don't know C that well.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    23
    Well that ends it at a string

    Now I just need to know how to begin it at a string


    Code:
    do
    {
     
    getline(fin, foo, '\n' );
    cout<<foo<<endl;
    }
    while (foo != ";");
    it stops reading the text after ; is found now I need to start reading the text when "a" is found

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    char blah;
    string foo, string1;
    
    do {
       inFile.get(blah);
       } while (blah != 'a');
    
    string1 = blah;
    
    getline(inFile, foo, '\n');
    
    string1 += foo;
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    23
    So I belive this is the last question If I find the starting string and ending string how would I go about pulling all the data from the middle. the way I was doing it before will not work with the other code.

    what I was doing is looking for keywords per line and what erver fallowed I captured but for some reson that is not working now.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Paste your code.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    23
    well I thought it was working but I geuss not when I ran another test.

    Code:
    string test,name;
    
    name = "BallSpin";
    
    do {
       getline(fin,test);
       cout<<test<<endl;
       system("PAUSE");
       } while (test != name);
    Should this not end when it comes to string BallSpin?

    also in my text file it is "AnimName BallSpin" so i might have to include the whole line instead of just the Key word "BallSpin"
    Last edited by kingpinzs; 12-11-2005 at 03:46 PM.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    It should exit the loop when your string says BallSpin, but I noticed you didn't put a delimeter on your getline function. That would by default make getline read until the first \n. Unless your file is simply "BallSpin" it wouldn't work.
    Code:
    getline(fin,test,' ');
    Last edited by SlyMaelstrom; 12-11-2005 at 03:51 PM.
    Sent from my iPadŽ

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    getline's delimiter defaults to the newline.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Yeah, I just checked and noticed that. In any event, he more than likely wants a space as the delimeter.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    May 2005
    Posts
    23
    well when I use getline(fin,test,' '); it does not exit

    but when I include the whole line and use
    getline(fin,test); it will exit now. I can work with that.

    so when it exits how do i pickup after were I left off at with out including were it left off? I need there very next line.

    edit:
    I even tryed '\n' and it still did not work.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    
     
     string foo, string1;
    
    string test,name,name2,name3;
    //BallSpin
    
    
    
    int main(int argc, char *argv[])
    {   name2 = "AnimName ";
        name = "BallSpin";
       name3 = name2 + name;
       fstream fin("image1.txt");
    
    
    do {
       getline(fin,test,'\n');
       cout<<test<<endl;
       } while (test != name3);
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Code:
    AnimName BallExplode
    File 1.bmp 
    Left 1
    Right 1
    Top 1
    Bottom 1 
    Red 1
    Green 1
    Blue 1
    Delay 1
    Width 1
    FrameW 1
    ;
    AnimName BallSpin
    File null.bmp 
    Left 0
    Right 92
    #Top 0
    Bottom 0 
    Red 0
    Green 0
    Blue 0
    Delay 0
    Width 0
    FrameW 0
    ;
    Last edited by kingpinzs; 12-11-2005 at 04:13 PM.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Ok, first off you're not declaring your variables. Secondly you declared an fstream, you should declare ifstream.

    The reason why '\n' or space doesn't work in this file is because if the delimeter was a newline character it'd read in "AnimName BallSpin" if it was a space, it'd read in "Ballspin\nFile"

    What you could do is either put a space after each word before you start a new line or you could resize your string after you read in a word so that it would only fit "BallSpin" and nothing more. There are probably numerous other ways to handle this.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    May 2005
    Posts
    23
    so in the text file that is posted how would you pull the data out?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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