This works well, but was just wondering if there are versions of ifstream that can getline into a string class so that I don't have to know the size of a max line length before hand.
I realize that there aren't really situations where defining MAX_LINE_LEN large enough won't work, but was just wondering if such a version of reading a line does exist.
void main()
{
string fname("myfile.txt");
ifstream fin(fname.c_str());
while (fin.good())
{
string line = "";
char ch = 0;
while (!fin.eof() && ch != '\n')
{
fin.get(ch);
if (!fin.eof() && ch != '\n')
line += ch;
}
cout << line << endl;
}
fin.close();
}
I'm not sure if I am doing this as effeciently as could be, so did try your suggestion codekiddy but don't understand how to use this class in context of the ifstream class.
I was hoping I could simply try fin.getline(sline)
where sline is of type istringstream or ostringstream.
Do you have an example for me in this context that demonstrates use of these classes to achieve desired result.