如何逐字符读取文件的全部内容? [英] How to read the entire content of a file character by character?

查看:129
本文介绍了如何逐字符读取文件的全部内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个包含如下数据的文件:

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.

这篇关于如何逐字符读取文件的全部内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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