Thread: Using input stream to get string from file.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    15

    Using input stream to get string from file.

    Basically, I'm trying to open a file that has 10 words/ phrases in it. What I want to do is open the file and pull just one of the phrases out(at random). I'm stuck at this point and am not really sure where to go from here.

    Any ideas?


    Code:
    #include <iostream>
    #include <string>
    #include <fstream> 
    
    using namespace std;
    
    int main ()
    
    {
    	
    	string random;
    
    	ifstream inFile;
    	inFile.open("random.dat");
    
    
    	inFile >> random;
    	
    	cout << random << endl;
    
    	inFile.close();
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read in all the phrases, then use (say) rand to choose one of them in an unpredictable fashion.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    "rand" as in random number generator?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    To read in all the phrases do I have to declare them as strings? Then read them in or can I just read them in?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CPPN0OB View Post
    "rand" as in random number generator?
    Sure!
    Quote Originally Posted by CPPN0OB View Post
    To read in all the phrases do I have to declare them as strings?
    That is the data type that deals with bunches of characters in one glob, yes.
    Quote Originally Posted by CPPN0OB View Post
    Then read them in or can I just read them in?
    I'm going to take a guess and say "yes".

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by tabstop View Post
    Read in all the phrases, then use (say) rand to choose one of them in an unpredictable fashion.
    if you know that there are 10 lines in file - use rand to generate random number and read only required number of lines... (why to read and store all 10 lines if you know you need line number 2)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Quote Originally Posted by vart View Post
    if you know that there are 10 lines in file - use rand to generate random number and read only required number of lines... (why to read and store all 10 lines if you know you need line number 2)
    How do I read in a certain line?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by CPPN0OB View Post
    How do I read in a certain line?
    not certain line, certain number of lines, storing only the last
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM