Looking for a tutorial

Im looking for a tutorial on reading a txt file into a vector. If someone could give me a link or post an example here that would be great.
Look at:

http://www.cplusplus.com/reference/iostream/fstream/
and
http://www.cplusplus.com/reference/stl/vector/

Then you should be able to figure out how to do that. :)
well this is what i have

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
string file;
int size;
vector <char> vector1;

cout << "What file to open? ";
cin >> file;
ifstream x (file.c_str());

// get size of file
x.seekg(0, ios::end);
size = x.tellg();
x.seekg(0, ios::end);

// allocate memory
vector1.resize(size);

// copy x into the vector
x.read (&vector1.begin(), size);

return 0;
}

but i get an error on the x.read line, what am i doing wrong?
Please use code tags in your posts (# in the format area to the right of where you type your posts); it makes the code easier to read.

Your problem with read is that vector does not work like that. Looking at the references firedraco provided, you can see that the first parameter needs to be of type char* and vector::begin() returns an iterator to the beginning of the vector. You will probably have to use a loop to get data into a temporary variable first then transfer it to the vector until you are finished reading the file.

Also, whenever you call open(), you should check to ensure it succeeded and close the file when you are finished. In addition, cin does not work with strings. Instead, use getline(cin, file);
Last edited on
A std::vector<char> is not as handy as a simple std::string. You can read an entire file into a string using a single getline():
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
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int complain( const string& message )
  {
  cerr << message << endl;
  return 1;
  }

string loadfile( ifstream& inf )
  {
  stringstream ss;
  inf.get( ss );
  return ss.str();
  }

int main()
  {
  string filename;
  cout << "filename> ";
  getline( cin, filename );

  ifstream f( filename.c_str() /*, ios::binary */ );  // if binary I/O is needed
  if (!f) return complain( string( "Couldn't open file" ) +filename );

  string s = loadfile( f );
  f.close();

  cout << "The file contains:\n" << s;

  return 0;
  }

Another useful option is a std::vector<std::string>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

...

vector <string> loadfile( ifstream& inf )
  {
  vector <string> result;
  string line;
  while (getline( inf, line ))
    result.push_back( line );
  return result;
  }

int main()
  {
  ...
  vector <string> all_lines_of_text = loadfile( f );
  ...
  }


Hope this helps.
Its been a couple days but I thought id post that i solved my problem myself,
x.read (&vector1.begin(), size); simply needed to be changed to x.read (&(vector1[0]), size);. Thanks for the input anyway Duaos
Topic archived. No new replies allowed.