home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!unixhub!ditka!eagercon!eagercon!eager
- From: eager@eagercon.com (Michael J. Eager)
- Subject: Re: More File read problems. Help I'm new and confused...
- Message-ID: <1992Dec19.181840.19871@eagercon.com>
- Sender: root@eagercon.com (Operator)
- Reply-To: eager@eagercon.com
- Organization: Eager Consulting
- References: <1gl9bhINN3f7@mirror.digex.com>
- Date: Sat, 19 Dec 1992 18:18:40 GMT
- Lines: 69
-
- In article 1gl9bhINN3f7@mirror.digex.com, jhurwitz@access.digex.com (j. hurwitz) writes:
- >
- >-------------------------- cut here -------------------------
- >
- >//programm to read a C source file and verify # of left to right braces
- >#include <stdio.h>
- >
- >int main(int argc, char *argv[])
- >{
- > FILE *fptr;
- > int l=0, r=0, b=0, j=0;
- > char ch;
-
- Change this to int ch. EOF is usually a value that cannot be stored in a char.
- You will usually hang at the end of the file.
-
- >
- > fptr = fopen("chkbrac.c","r");
- >
- > while ((ch =getc(fptr)) !=EOF)
- > {
- > j=0;
- > switch (ch)
- > {
- > case '{': b++;
- > break;
- > case '}': b--;
- > break;
- > case '/*': while(j==0)
- > { (ch=getc(fptr)); if (ch='*/') j=1; }
-
- ^^^^ ^^^^
- First, ch will be one character, not two. Second, the compiler is probably
- generating a 16-bit value for the case, so you will never execute this case.
-
- You need to have a case for '/' and then, if the next character is a '*' skip
- until you get a '*' followed by a '/'.
-
- > break;
- > case '\'': while(j==0)
- > { (ch=getc(fptr)); if (ch='\'') j=1; }
-
- Better coding style (at least more ideomatic) is
- while (getc(fptr) != '\'') ;
- No unneeded variables used.
-
- > break;
- > case '\"': while(j==0)
- > { (ch=getc(fptr)); if (ch='\"') j=1; }
- > break;
- > } /* Junk to test program */
- > } /* { {{{ '}' "}..."*/
- > if (b==0)
- > printf("\nBraces match !!!\n");
- > else printf("\nBraces do not match\n");
- > fclose(fptr);
- >}
-
-
- Other than this, the program seems to work. It ignores comments, as
- mentioned above, but doesn't seem to have any other problems.
-
-
-
- ---
- Michael J. Eager Michael.Eager@eagercon.com
- Eager Consulting (415) 325-8077
- 1960 Park Boulevard, Palo Alto, CA 94306-1141
-
-