How to Read from a Text File, Character by Character in C++(如何在 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++ 中逐个字符地从文本文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 C++ 中逐个字符地从文本文件中读取
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- STL 中有 dereference_iterator 吗? 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01