Reading file to Vector

Nov 26, 2016 at 8:11pm
Hello, im having trouble reading the text file to vector. And writing what is read into another file. Can someone help. Thanks.

As of now, it only reads the first line and print that 1st line to output file.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	const int SIZE = 25;
	ifstream dataFile;
	ofstream outFile;
	string line;
	vector<string> quiz(SIZE);

	dataFile.open("QuestionBank.txt");

	if (!dataFile)				//if else statment to display error message for corrupted inout file, if no error proceed.
		cout << "Error opening data file\n";
	else
	{  

		for (int i = 0; i < SIZE; i++)
		{
			getline(dataFile, line);
			quiz.push_back(line);
			
			outFile.open("stats.txt");		//Print gathered max, min and avg. values to a sperate new file called, stats.txt
			outFile << line << endl;	// Comments displayed in file.

		}
			dataFile.close();
		
	}

	cout << endl;

	cout << "Welcome to Program \n";
	cout << "------------------------\n";
	cout << endl;

	system("pause");

	return 0;
}
Last edited on Nov 26, 2016 at 8:14pm
Nov 26, 2016 at 9:55pm
Better use getline like this, so you don't leave out data inside your files. It will ensure that all strings will be pushed onto the vector.

1
2
3
4
std::string line;

while(std::getline(file, line, ' ')
quiz.push_back(line);
Nov 26, 2016 at 10:46pm
Like this?

But if i do this, i get an error. Red sguilly lines appear under quiz.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (!dataFile)				//if else statment to display error message for corrupted inout file, if no error proceed.
		cout << "Error opening data file\n";
	else
	{  

		//for (int i = 0; i < SIZE; i++)
		//{
		//	getline(dataFile, line);
		//	quiz.push_back(line);

		//	outFile.open("Answers.txt");		
		//	outFile << "NEW        : " << line << endl;	// Comments displayed in file.
		//}

		while (getline(dataFile, line, ' ')
			quiz.push_back(line);

		dataFile.close();
	}
Last edited on Nov 26, 2016 at 10:46pm
Nov 26, 2016 at 10:56pm
Ok, so i've figured out the error.

But this only prints the 1st line.
There are over 10 lines in the file.
Nov 26, 2016 at 11:03pm
In your first post you were opening stats.txt INSIDE the for loop - I.e repeatedly. It needs to be before that loop.
Nov 26, 2016 at 11:17pm
Like this?

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

		for (unsigned int i = 0; i < quiz.size(); i++)
		{
			/*getline(dataFile, line);
			quiz.push_back(line);*/

			while (getline(dataFile, line))

			
				quiz.push_back(line);
			

				
		}
		outFile.open("Answers.txt");		
		outFile << "Hello        : " << line << endl;	
		

		dataFile.close();
Nov 27, 2016 at 12:57am
Split your program up into separate functions to make it easier to read.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
vector<string> read_file( const string& filename )
{
    ifstream ifs{ filename };

    // throw when ifs.bad( ), indicates something catastrophic
    ifs.exception( ifs.exception( ) | ios_base::badbit )

    // #include <exception>
    if( !ifs ) throw exception{ "can't open " + filename };

    string line{}, vector<string> quiz{};
    while( getline( ifs, line ) ) {
        // read the whole file
        if( ifs.eof( ) ) return quiz;

        if( ifs.fail( ) )
            throw exception{ "failed to read " + filename };

        quiz.push_back( line );
    }

    if( ifs.fail( ) )
        throw exception{ "failed to read " + filename };
}

void keep_window_open( )
{
    cout << "Enter a character to exit ";
    char c{};
    cin >> c;
}

int main( )
{
    try {
        const string filename{ "QuestionBank.txt" };

        vector<string> quiz{ read_file( filename ) };
    }
    catch( const exception& e ) {
        cout << "exception caught: " << e.what( ) << '\n';
        keep_window_open( );
    }
    catch( ... ) {
        cout << "unhandled exception caught\n";
        keep_window_open( );
    }

    keep_window_open( );
}
Last edited on Nov 27, 2016 at 1:03am
Nov 27, 2016 at 8:54am
Like this? 


Well yes, OK. As long as you don't keep trying to open the file within a loop.

Can you show us the latest version of your code (in full) and state the latest version of what is wrong with it (if you haven't fixed it already).
Nov 28, 2016 at 3:25am
Thank you. i figured it out. It was the loop. Thank you.
Topic archived. No new replies allowed.