0

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

Mike
  • 477
  • 2
  • 7
  • 24

2 Answers2

1
#include <fstream>

std::ifstream file("filename.txt");

StuffType stuff;
while(file >> stuff)
{
    // If you are here you have successfully read stuff.
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
David
  • 27,652
  • 18
  • 89
  • 138
1

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.

physicalattraction
  • 6,485
  • 10
  • 63
  • 122
  • I tried that too, but I couldn't find the right syntax. Perhaps you can help? This does at least NOT work: std::string buf; file.getline(buf, 256); Neither does it work if you remove the second argument in getline. – physicalattraction Jul 02 '12 at 13:44
  • 1
    `getline` for `string` is not a member. The syntax is `std::getline( file, buf );` – Potatoswatter Jul 03 '12 at 01:37