I am looking to write a code where, it reads each charachter from a file(display 20 lines at a time in a out file) and store the one i want and ignore the rest.
curly brace check(so the program is equally balanced.)
I am having a hard time with the logic how to get the braces and ignore the others( //, "{}", '{'...)
You start in "normal" mode. In normal mode you take into account braces you encounter: Opening braces account for a positive imbalance, while closing ones account for a negative imbalance. In the end, your count must equal zero or there is imbalance somewhere.
If during the course of your reading you encounter a single line comment (2 consecutive /'s), you update the status to "Single-line comment" where any and all braces are ignored. You return to normal state once you hit the newline char.
If you encounter /*, then you update the status from "normal" to "multiline comment", where any and all braces are ignored; single line comments too are ignored, newline chars are ignored, and status is returned to "normal" only after */ is encountered.
And you probably get the idea. Define states and the rules that apply to each state and when/how to move from one state to the other. Welcome to text parsing!
Why are you doing this? You need to be able to parse a C or C++ source file pretty well (handling things beyond simple braces -- strings, characters, macros, and commentary) which is a pretty advanced topic. (C and C++ syntaxes are known to be difficult to parse.)
Sorry, no code sample from me. I'm tight on time and this is no simple thing. You'll have to get your head around it. You need to teach your reader how to properly read C++ and my idea is to keep track of where you are (inside a single line comment, inside a multi-line comment, inside a string literal, etc.) so you can take appropriate action, like ignoring braces or not, or determine when you're out of a comment or a string literal.
A good way to do that is to have a simple "recursive descent"-type parser. Start normally in the main function. If you encounter a single or double-quote, call the "read/skip string" function. If you encounter a comment opening sequence, call the "read/skip comment" function. Etc. All that remains is to match parentheses in the main function. (You'll need a stack.)