Despot Despondency
January 10th, 2009, 01:33 PM
Hey, I'm having a problem with my program that is meant to read a file into a vector<string>. Here's the code
#include "read_in_file.h"
#include <fstream>
#include <iostream>
using namespace std;
vector<string> read_in_file_vec(string filename){
string line;
ifstream file (filename.c_str());
vector<string> text;
if(file.is_open()){
while(!file.eof()){
getline(file,line);
cout << line << endl;
text.push_back(line);
}
file.close();
}
else
cout << "Could not open file" << endl;
}
Now when I run the code on a file called "teams" I get the following output
Liverpool
Chelsea
Man Utd
Aston Villa
Arsenal
Everton
Wigan
Hull
Fulham
West Ham
Bolton
Portsmouth
Man City
Newcastle
Sunderland
Tottenham
Middlesbrough
Stoke
Blackburn
West Brom
Segmentation fault
It seems to be a problem with the end of file, as 'West Brom' is the last line in the file. I don't understand why I'm getting this error. Any help would be appreciated.
#include "read_in_file.h"
#include <fstream>
#include <iostream>
using namespace std;
vector<string> read_in_file_vec(string filename){
string line;
ifstream file (filename.c_str());
vector<string> text;
if(file.is_open()){
while(!file.eof()){
getline(file,line);
cout << line << endl;
text.push_back(line);
}
file.close();
}
else
cout << "Could not open file" << endl;
}
Now when I run the code on a file called "teams" I get the following output
Liverpool
Chelsea
Man Utd
Aston Villa
Arsenal
Everton
Wigan
Hull
Fulham
West Ham
Bolton
Portsmouth
Man City
Newcastle
Sunderland
Tottenham
Middlesbrough
Stoke
Blackburn
West Brom
Segmentation fault
It seems to be a problem with the end of file, as 'West Brom' is the last line in the file. I don't understand why I'm getting this error. Any help would be appreciated.