Advise:

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( //, "{}", '{'...)

Please help with code or hints.

thanks.
I have no idea what you actually want.
I am writing a code to check if the braces are balanced, but I am confused on how to eliminate:

1. // {} inside a comment
2. /* {} inside block comment */
3. braces that follow a backslash '\{' or "{", or '}'

What would be used in getting the {, } and store it.

and display 15 lines at a time.

1
2
3
4
5
6
  count = 0;
  .getc(inF, brace)

  if ((brace=='{') || (brace =='}'))
  count ++;


But do not know how code so it ignores the possible wrong input.
You normally do this kind of thing with a finite state machine:
http://en.wikipedia.org/wiki/Finite-state_machine
Define and track the state of your reader object:

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!
can you put an example:
I am lost when you mention update status to.....and return to normal again.

I just cant get that to a code...
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.)

Why not use a compiler? Or lint?
http://www.gimpel.com/html/index.htm
http://sourceforge.net/apps/mediawiki/cppcheck/
http://legacy.cleanscape.net/products/cpp/index.html
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.)

Good luck!
Topic archived. No new replies allowed.