reading from a textfile into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajenia
    New Member
    • Oct 2006
    • 8

    reading from a textfile into an array

    i need to write a program in c++ that is going to read English sentences from a textfile, a line at a time and store them in an array. the file has 100 sentences each occupying a single line.
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by sajenia
    i need to write a program in c++ that is going to read English sentences from a textfile, a line at a time and store them in an array. the file has 100 sentences each occupying a single line.
    And what is it you have difficulties with?

    Comment

    • sajenia
      New Member
      • Oct 2006
      • 8

      #3
      Originally posted by arne
      And what is it you have difficulties with?
      having problems with array declaration. is it suppossed to be a one-dim array or a two-dim array?
      i have managed to have the program read from a textfile but it is not storing into an array. displaying the senteces has to be from an array not from the textfile as it is doing.

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by sajenia
        having problems with array declaration. is it suppossed to be a one-dim array or a two-dim array?
        i have managed to have the program read from a textfile but it is not storing into an array. displaying the senteces has to be from an array not from the textfile as it is doing.
        One dimensional: it could be an array of strings like this
        Code:
        vector<strings> sentences;

        Comment

        • sajenia
          New Member
          • Oct 2006
          • 8

          #5
          Originally posted by arne
          One dimensional: it could be an array of strings like this
          Code:
          vector<strings> sentences;

          i don't even understand this line of code that u've sent. can u elaborate. what is a vector anyway?

          Comment

          • arne
            Recognized Expert Contributor
            • Oct 2006
            • 315

            #6
            Originally posted by sajenia
            i don't even understand this line of code that u've sent. can u elaborate. what is a vector anyway?
            A vector is a container class from the C++ Standard Template library (STL). All you need to know for practical use, however, is that a vector can be used to store a sequence of values of a certain type, that it grows automatically if you need more space and that it grants efficient access. It's similar to a simple array, but somewhat more comfortable.

            For example,
            Code:
            vector<string> vec_of_str;
            vector<int> vec_of_int;
            vector<double> vec_of_dbl;
            define vectors that hold elements of type string, int and double, respectively.
            BTW, string is also a type from the STL and no built-in type like int or float.
            Please refer also to http://www.cppreference.com to get an idea what things you can do with vectors and strings.

            Here comes the code that may do what you need. Have a look and try to understand it (I added some comments :))

            Code:
            #include <iostream>
            #include <fstream>
            #include <string>
            #include <vector>
            
            using namespace std;
            
            int main( void ) 
            {
            	// define an infile stream
            	ifstream fin( "textfile.txt" );
            
            	// define a vector of strings
            	vector<string> sentences;
            
            	//define a temporary string
            	string tmp_str;
            
            	// read the file line by line into tmp_str
            	while( getline( fin, tmp_str ) ) {
            		
            		// append the tmp_str as a new last 
                            // element to sentences 
            		sentences.push_back( tmp_str );
            	}
            	
            	// print out the 3rd element of our vector
            	cout << sentences[2];
            
            	return 0;
            }

            Comment

            Working...