I'm new to C and I'm trying to read in a file that can have an arbitrary number of lines, and put each line into a string array. I guess my problem is that I dont't want to have to explicitly create a new array with a certain size since the number of lines can always change (in the code below I use 101). Is there a better way to return an array or a pointer to an array that contrains all the lines. This is what I have so far.
Expand|Select|Wrap|Line Numbers
- string* test()
- {
- string line;
- string *vol= new string[101];
- int i = 0;
- ifstream myfile("VOL.txt", ios::in);
- if (myfile.is_open())
- {
- while(!myfile.eof() && i != 101)
- {
- getline (myfile, line);
- if (line != firstLine)
- {
- vol[i] = line;
- i++;
- }
- }
- myfile.close();
- }
- return vol;
- }