C++ 从 fstream 读取单个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9176867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a single character from an fstream?
提问by Jcrack
I'm trying to move from stdio to iostream, which is proving very difficult. I've got the basics of loading a file and closing them, but I really don't have a clue as to what a stream even is yet, or how they work.
我正在尝试从 stdio 转移到 iostream,事实证明这非常困难。我已经掌握了加载文件和关闭文件的基础知识,但我真的不知道流到底是什么,或者它们是如何工作的。
In stdio everything's relatively easy and straight forward compared to this. What I need to be able to do is
与此相比,在 stdio 中,一切都相对简单直接。我需要做的是
- Read a single character from a text file.
- Call a function based on what that character is.
- Repeat till I've read all the characters in the file.
- 从文本文件中读取单个字符。
- 根据该字符是什么调用函数。
- 重复直到我读完文件中的所有字符。
What I have so far is.. not much:
到目前为止我所拥有的是..不多:
int main()
{
std::ifstream("sometextfile.txt", std::ios::in);
// this is SUPPOSED to be the while loop for reading. I got here and realized I have
//no idea how to even read a file
while()
{
}
return 0;
}
What I need to know is how to get a single character and how that character is actually stored(Is it a string? An int? A char? Can I decide for myself how to store it?)
我需要知道的是如何获取单个字符以及该字符的实际存储方式(它是字符串?int?一个字符?我可以自己决定如何存储它吗?)
Once I know that I think I can handle the rest. I'll store the character in an appropriate container, then use a switch to do things based on what that character actually is. It'd look something like this.
一旦我知道我认为我可以处理剩下的事情。我会将角色存储在适当的容器中,然后使用开关根据该角色的实际情况执行操作。它看起来像这样。
int main()
{
std::ifstream textFile("sometextfile.txt", std::ios::in);
while(..able to read?)
{
char/int/string readItem;
//this is where the fstream would get the character and I assume stick it into readItem?
switch(readItem)
{
case 1:
//dosomething
break;
case ' ':
//dosomething etc etc
break;
case '\n':
}
}
return 0;
}
Notice that I need to be able to check for white space and new lines, hopefully it's possible. It would also be handy if instead of one generic container I could store numbers in an int and chars in a char. I can work around it if not though.
请注意,我需要能够检查空白和新行,希望这是可能的。如果我可以将数字存储在 int 中并将字符存储在 char 中,而不是一个通用容器,那也会很方便。如果没有,我可以解决它。
Thanks to anyone who can explain to me how streams work and what all is possible with them.
感谢任何可以向我解释流是如何工作的以及它们可以做什么的人。
采纳答案by Xeo
You also can abstract away the whole idea of getting a single character with streambuf_iterator
s, if you want to use any algorithms:
streambuf_iterator
如果您想使用任何算法,您还可以抽象出使用s获取单个字符的整个想法:
#include <iterator>
#include <fstream>
int main(){
typedef std::istreambuf_iterator<char> buf_iter;
std::fstream file("name");
for(buf_iter i(file), e; i != e; ++i){
char c = *i;
}
}
回答by jrok
You can also use standard for_each
algorithm:
您还可以使用标准for_each
算法:
#include <iterator>
#include <algorithm>
#include <fstream>
void handleChar(const char& c)
{
switch (c) {
case 'a': // do something
break;
case 'b': // do something else
break;
// etc.
}
}
int main()
{
std::ifstream file("file.txt");
if (file)
std::for_each(std::istream_iterator<char>(file),
std::istream_iterator<char>(),
handleChar);
else {
// couldn't open the file
}
}
istream_iterator
skips whitespace characters. If those are meaningful in your file use istreambuf_iterator
instead.
istream_iterator
跳过空白字符。如果这些在您的文件中有意义,请istreambuf_iterator
改用。
回答by SigTerm
Next time you have similar problem go to cplusplusreferenceor similar site, locate classyou have problem with and read description of every method. Normally, this solves the problem. Googling also works.
下次遇到类似问题时,请访问cplusplusreference或类似站点,找到有问题的类并阅读每个方法的说明。通常,这可以解决问题。谷歌搜索也有效。
回答by batburger
This has already been answered but whatever. You can use the comma operatorto create a loop which behaves like a for each loop which goes through the entire file reads every character one by one and stop when it's done.
这已经得到了回答,但无论如何。您可以使用逗号运算符创建一个循环,它的行为类似于 for each 循环,它遍历整个文件,逐个读取每个字符,并在完成后停止。
char c;
while((file.get(c), file.eof()) == false){
/*Your switch statement with c*/
}
Explanation:
The first part of the expression in the for loop (file.get(c), file.eof())
will function as follows. Firstly file.get(c)
gets executed which reads a character and stores the result in c
. Then, due to the comma operator, the return value is discarded and file.eof() gets executed which returns a bool whether or not the end of the file has been reached. This value is then compared.
说明:for 循环中表达式的第一部分的(file.get(c), file.eof())
作用如下。首先file.get(c)
被执行,它读取一个字符并将结果存储在c
. 然后,由于逗号运算符,返回值被丢弃并执行 file.eof() ,无论是否已到达文件末尾,它都会返回一个布尔值。然后比较该值。
Side Note:ifstream::get()
always reads the next character. Which means calling it twice would read the first two character in the file.
旁注:ifstream::get()
总是读取下一个字符。这意味着调用它两次将读取文件中的前两个字符。
回答by mikithskegg
while (textFile.good()) {
char a;
textFile.get(a);
switch(a)
{
case 1:
//dosomething
break;
case ' ':
//dosomething etc etc
break;
case '\n':
}
}