使用 ifstream 读取文件
我正在尝试从文件中读取: 该文件是多行的,基本上我需要检查每个“单词”。词是任何非空间的东西。
示例输入文件为:
示例文件:
测试二维
字3.5
输入
{
测试 13.5 12.3
另一个{
测试 145.4
}
}
我尝试了这样的事情:
ifstream inFile(fajl.c_str(), ifstream::in);
if(!inFile)
{
cout << "Cannot open " << fajl << endl;
exit(0);
}
string curr_str;
char curr_ch;
int curr_int;
float curr_float;
cout << "HERE\n";
inFile >> curr_str;
cout << "Read " << curr_str << endl;
问题是当它读取新行时它就会挂起。我在测试 13.5 之前阅读了所有内容 但一旦到达该线,它就不会做任何事情。 任何人都可以告诉我我做错了什么? 关于如何做到这一点有更好的建议吗???
我基本上需要浏览文件并当时浏览一个“单词”(非白色字符)。 我
谢谢
I am trying to read from file:
The file is multiline and basically i need to go over each "word". Word being anything non space.
Sample input file would be:
Sample file:
test 2d
word 3.5
input
{test 13.5 12.3
another {
testing 145.4
}
}
So I tried something like this:
ifstream inFile(fajl.c_str(), ifstream::in);
if(!inFile)
{
cout << "Cannot open " << fajl << endl;
exit(0);
}
string curr_str;
char curr_ch;
int curr_int;
float curr_float;
cout << "HERE\n";
inFile >> curr_str;
cout << "Read " << curr_str << endl;
The problem is when it reads new line it just hangs. I read everything before test 13.5
but once it reaches that line it doesnt do anything.
Anyone can tell me what I am doing wrong?
Any better suggestion on how to do this???
I essentially need to go through file and go one "word" (non white char) at the time.
I
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您打开文件“inFile”但正在从“std::cin”读取任何特殊原因?
You open a file 'inFile' but are reading from the 'std::cin' any particular reason?
不确定这在 iostream 库的“精神”中如何,但您可以使用未格式化的输入来做到这一点。比如:
这有效吗?
Not sure how "in the spirit" of the iostream library this is, but you could do it with unformatted input. Something like:
Does that work?