OP's code does not check for success of any of their file IO, so the file may not have opened, the file may be empty, and the read may have failed for any number of reasons.
Fortunately, getline
returns the input stream and streams implement a really neat operator bool that returns false if the stream is in an error condition and could not be read. If the file could not be read, the contents of temp are most certainly not valid and should not be used.
So...
void readfile(vector<string> &filename)
{
string temp;
ifstream infile(filename[2].c_str());
if (getline (infile, temp)) //test the read for success
{
cout << temp << endl;
}
else
{
cout << "failed to read file" << endl;
}
}
If getline fails for any reason, including file not open, file is empty, and file is corrupt and unreadable, the stream's state will be marked bad and return false when checked by if()
.
Normally at this point you should check the type of error, infile.clear()
the stream to remove the error condition, and pick up the pieces, but in this case there isn't much point. If you can't read the beginning of a file into a string, you got big problems and should take a closer look at the health and contents of file filename[2]
.
By the way, if your compiler is relatively up to date, ifstream's constructor will eat a std::string and ifstream infile(filename[2]);
will be valid.
Style-wise, you're better off just passing the filename string into readfile, not the vector. This allows you to reuse the readfile
function for more than just element 2 of a vector.
void readfile(string & filename)
{
string temp;
ifstream infile(filename);
if (getline (infile, temp)) //test the read for success
{
cout << temp << endl;
}
else
{
cout << "failed to read file " << filename << endl;
}
}
And call with
readfile(filename[2]);
Expanding this function to meet OP's true goal
void readfile(string & filename,
vector<string> & strings)
{
string temp;
ifstream infile(filename);
if (getline (infile, temp)) //test the read for success
{
strings.push_back(temp);
cout << temp << endl;
}
else
{
cout << "failed to read file " << filename << endl;
}
}
and call with
vector<string> strings;
...
readfile(filename[2], strings);