Using input stream to get string from file.

This is a discussion on Using input stream to get string from file. within the C++ Programming forums, part of the General Programming Boards category; Basically, I'm trying to open a file that has 10 words/ phrases in it. What I want to do is ...

  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,185
    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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 08: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, 01:11 AM

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