如何只从文件中读取特定字符?

时间:2013-11-28 19:23:26

标签: c++ file-io char stack

我正在研究一个项目,遇到了我认为是我忽略了一个简单的操作或什么的。

问题的一个例子是从指定文件中查找'%'或'*'字符。

当它们被找到时,我会将它们推到堆栈上,然后移动到文件中的下一个字符。

例如

ifstream fin;
fin.open( fname );

while ( fin.get(singlechar)){      //char singlechar;

if (singlechar == '(' || singlechar == ')' || singlechar == '{' || singlechar == '}' || > singlechar == '[' || singlechar == ']')

    Stack::Push(singlechar);    //push char on stack

这样做的好方法是什么? for循环,while while循环? getline而不是singlechar?

1 个答案:

答案 0 :(得分:0)

existing question已有答案。这里:

char ch;
fstream fin(filename, fstream::in);
while (fin >> noskipws >> ch) {
    cout << ch; // Or whatever
    //In your case, we shall put this in the stack if it is the char you want
    if(ch == '?') {
        //push to stack here
    }
}

所以基本上,如果对应于此,则将char保存在堆栈中。

相关问题