使用ifstream读取文件 [英] Reading through file using ifstream

查看:174
本文介绍了使用ifstream读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件读取:
文件是多行的,基本上我需要经过每个字。词是任何非空间。

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:

测试2d

字3.5

输入

{


test 13.5 12.3

另一个{

测试145.4

}

}

test 2d
word 3.5
input
{

test 13.5 12.3
another {
testing 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
但一旦它到达那行,它不做任何事情。
任何人都可以告诉我我做错了什么?
任何更好的建议,如何做到这一点

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

I essentially need to go through file and go one "word" (non white char) at the time. I

感谢

推荐答案

'inFile'但是从'std :: cin'中读取任何特定原因?

You open a file 'inFile' but are reading from the 'std::cin' any particular reason?

/*
 * Open the file.
 */
std::ifstream   inFile(fajl.c_str());   // use input file stream don't.
                                        // Then you don't need explicitly specify
                                        // that input flag in second parameter
if (!inFile)   // Test for error.
{
    std::cerr << "Error opening file:\n";
    exit(1);
}

std::string   word;
while(inFile >> word)  // while reading a word succeeds. Note >> operator with string
{                      // Will read 1 space separated word.
    std::cout << "Word(" << word << ")\n";
}

这篇关于使用ifstream读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆