【问题标题】:How to read in specific characters only from a file?如何仅从文件中读取特定字符?
【发布时间】:2013-11-28 19:23:25
【问题描述】:

我正在做一个项目,遇到了我认为是我忽略了一个简单的操作之类的问题。

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

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

例如

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循环? getline 而不是 singlechar?

【问题讨论】:

    标签: c++ file-io char stack


    【解决方案1】:

    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
        }
    }
    

    所以基本上,如果对应的字符,则将其保存在堆栈中。

    【讨论】:

      猜你喜欢
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      相关资源
      最近更新 更多