简体   繁体   中英

How to read in specific characters only from a file?

I am working on a project, encountered what I consider to be me being overlooking a simple operation or something.

An example of the problem would be looking for the '%' or '*' characters from a specified file.

I will be pushing them down onto a stack when they are located, then moving onto the next character in the file.

for example

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

What would be a good way to do this? for loop, do while loop? getline instead of singlechar?

There is an answer from an existing question already. Here:

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

So basically, you save the char in the stack if it corresponds to that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM