How to use IStringStream to read from a file?
-
18-06-2021 - |
Question
I need to read in an expression from a file using a string stream and convert the expression to into another form. But I am having trouble figuring out how to read lines from the file using the Istringstream. Can anyone help me with the #includes and syntax for this? Thanks
La solution
#include <fstream>
std::ifstream file("filename.txt");
StuffType stuff;
while(file >> stuff)
{
// If you are here you have successfully read stuff.
}
Autres conseils
As an addition to the answer of Dave above: to read one line from a file, you can use the following code:
char buf[256];
file.getline(buf,256);
The character string buf then contains the line of text in the file.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow