store text to array until text is over.

hello all,
this is part of my code. what i wanna know is if there is a way to fill this array until the text is over instead of having to set the number of strings in the file (20).
And then store that number into a variable.

thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
	//array:
	string words[20];
	
	
	//file to open:
	ifstream file("test.txt");
	
	//variables	
	int lengthOfWords;


	//opening the file	
	if (file.is_open())
	{
		for (int i = 0; i < 20; i++) //stores the words of the text file into array 
		{
			file >> words[i];					
		}

	}	
	else {cout << "The specified file does not exist! " << endl; } // returned if the file DNE.

}
//lengthOfWords would be the variable to store this int value 

Yes! What you are describing is called a vector. Just #include <vector> and create a temporary string ( string temp; ). string words[20]; would become vector<string> words;. You can then read into the temporary string, and use the vector function words.push_back ( temp ); to fill the vector. You can also change the for loop to while ( file >> temp ).
Topic archived. No new replies allowed.