2012-07-02 30 views
0

我需要使用字符串流从文件读入表达式,并将表达式转换为另一种形式。但是我很难弄清楚如何使用Istringstream从文件中读取行。任何人都可以帮助我的#includes和语法为此?由于如何使用IStringStream从文件中读取?

+4

你不能,你需要一个**文件**流从文件中读取,**字符串**流是用于字符串。 –

回答

1
#include <fstream> 

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

StuffType stuff; 
while(file >> stuff) 
{ 
    // If you are here you have successfully read stuff. 
} 
1

作为一个除了戴夫以上的答案是:从文件中读取一行,您可以使用下面的代码:

char buf[256]; 
file.getline(buf,256); 

字符串然后buf将包含一个文本行在文件中。

+0

为什么不读入'std :: string'? – Potatoswatter

+0

我也试过,但是我找不到合适的语法。也许你可以帮忙?这至少不会工作:std :: string buf; file.getline(buf,256);如果您删除getline中的第二个参数,它也不会起作用。 – physicalattraction

+1

'getline' for'string'不是成员。语法是'std :: getline(file,buf);' – Potatoswatter

相关问题