I have a function that reads a file and puts the content into a string. I like to clean the string so there's not white space and such. I tried ws >> but it didn't cut the cheese. If any one helped me I'd be --> :-D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
string file2String(const string &fileName)
{
ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);
if(!ifs)
{
cerr << "Error : File could not be opened." << endl;
exit(1);
}
ifstream::pos_type fileSize = ifs.tellg();
ifs.seekg(0, ios::beg);
ifs >> ws; //would like to remove whitespace, line breaks and such...
vector<char> bytes(fileSize);
ifs.read(&bytes[0], fileSize);
return string(&bytes[0], fileSize);
}
You can have a read-into string and a total string. Constantly read into the read-into string using the formatted >> operator because it ignores whitespace, and then add the read-into string to the total string. ;)