如何逐字符读取文件的全部内容?
问题描述:
有一个包含如下数据的文件:
There is a file containing data like the following:
11619.
Jody rosy.
32.
north st.
1/8/2013.
52.
我想按如下方式读取整个文件内容:
I would like to read the entire file content as follows:
1
1
6
1
9
.
J
o
d
y
etc.
我该如何读取文件内容?
How can I read the file content that way?
答
我会这样:
std::ifstream infile("myfile.txt");
char ch;
while(infile.get(ch))
{
... process ch ...
}
这避免了出现在最后一个字符上或在循环的第一次迭代之前必须读取一个字符的问题.
This avoids the problems that appear on the last character, or having to read a character before the first iteration of the loop.