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