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
Solution
#include <fstream>
std::ifstream file("filename.txt");
StuffType stuff;
while(file >> stuff)
{
// If you are here you have successfully read stuff.
}
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow