Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何仅从文件中读取特定字符? 我正在做一个项目,遇到我认为是我忽略了一个简单的操作或某事。_C++_File Io_Char_Stack - Fatal编程技术网

C++ 如何仅从文件中读取特定字符? 我正在做一个项目,遇到我认为是我忽略了一个简单的操作或某事。

C++ 如何仅从文件中读取特定字符? 我正在做一个项目,遇到我认为是我忽略了一个简单的操作或某事。,c++,file-io,char,stack,C++,File Io,Char,Stack,问题的一个示例是从指定文件中查找“%”或“*”字符 当它们被定位时,我将把它们下推到堆栈上,然后移动到文件中的下一个字符上 比如说 ifstream fin; fin.open( fname ); while ( fin.get(singlechar)){ //char singlechar; if (singlechar == '(' || singlechar == ')' || singlechar == '{' || singlechar == '}' || > si

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

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

比如说

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

这样做的好方法是什么?对于循环,请执行while循环?getline而不是singlechar?

已经有了一个答案。在这里:

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与之对应,则将其保存在堆栈中