如何从文本文件中读取,C ++中的字符 [英] How to Read from a Text File, Character by Character in C++

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

问题描述

我想知道是否有人可以帮助我找出如何从C ++中的文本文件读取,逐个字符。这样,我可以有一个while循环(仍然有文本),我在文本文档中的下一个字符存储在一个临时变量,所以我可以做一些事情,然后重复与下一个字符的过程。我知道如何打开文件和一切,但 temp = textFile.getchar()似乎不工作。提前感谢。

I was wondering if someone could help me figure out how to read from a text file in C++, character by character. That way, I could have a while loop (while there's still text left) where I store the next character in the text document in a temp variable so I could do something with it, then repeat the process with the next character. I know how to open the file and everything, but temp = textFile.getchar() doesn't seem to work. Thanks in advance.

推荐答案

您可以尝试以下方式:

char ch;
fstream fin("file", fstream::in);
while (fin >> noskipws >> ch) {
    cout << ch; // Or whatever
}

这篇关于如何从文本文件中读取,C ++中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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