home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18690 < prev    next >
Encoding:
Text File  |  1992-12-21  |  2.3 KB  |  82 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!unixhub!ditka!eagercon!eagercon!eager
  3. From: eager@eagercon.com (Michael J. Eager)
  4. Subject: Re: More File read problems. Help I'm new and confused...
  5. Message-ID: <1992Dec19.181840.19871@eagercon.com>
  6. Sender: root@eagercon.com (Operator)
  7. Reply-To: eager@eagercon.com
  8. Organization: Eager Consulting
  9. References: <1gl9bhINN3f7@mirror.digex.com>
  10. Date: Sat, 19 Dec 1992 18:18:40 GMT
  11. Lines: 69
  12.  
  13. In article 1gl9bhINN3f7@mirror.digex.com, jhurwitz@access.digex.com (j. hurwitz) writes:
  14. >
  15. >-------------------------- cut here -------------------------
  16. >
  17. >//programm to read a C source file and verify # of left to right braces
  18. >#include <stdio.h>
  19. >
  20. >int main(int argc, char *argv[])
  21. >{
  22. >   FILE *fptr;
  23. >   int l=0, r=0, b=0, j=0;
  24. >   char ch;
  25.  
  26. Change this to int ch.  EOF is usually a value that cannot be stored in a char.
  27. You will usually hang at the end of the file.
  28.  
  29. >
  30. >   fptr  = fopen("chkbrac.c","r");
  31. >
  32. >   while ((ch =getc(fptr)) !=EOF)
  33. >     {
  34. >     j=0;
  35. >     switch (ch)
  36. >       {
  37. >       case '{': b++;
  38. >                    break;
  39. >       case '}': b--; 
  40. >                    break;
  41. >       case '/*': while(j==0)
  42. >                { (ch=getc(fptr)); if (ch='*/') j=1; }
  43.  
  44.         ^^^^                                  ^^^^
  45. First, ch will be one character, not two.  Second, the compiler is probably 
  46. generating a 16-bit value for the case, so you will never execute this case.
  47.  
  48. You need to have a case for '/' and then, if the next character is a '*' skip
  49. until you get a '*' followed by a '/'.
  50.  
  51. >                break;
  52. >       case '\'': while(j==0)
  53. >                { (ch=getc(fptr)); if (ch='\'') j=1; }
  54.  
  55. Better coding style (at least more ideomatic) is
  56.             while (getc(fptr) != '\'') ;
  57. No unneeded variables used.
  58.  
  59. >                break;
  60. >       case '\"': while(j==0)
  61. >                { (ch=getc(fptr)); if (ch='\"') j=1; }
  62. >                break;
  63. >       }                     /* Junk to test program */
  64. >     }                       /* { {{{ '}' "}..."*/
  65. >   if (b==0)
  66. >    printf("\nBraces match !!!\n");
  67. >   else printf("\nBraces do not match\n");
  68. >   fclose(fptr);
  69. >}
  70.  
  71.  
  72. Other than this, the program seems to work.  It ignores comments, as 
  73. mentioned above, but doesn't seem to have any other problems. 
  74.  
  75.  
  76.  
  77. ---
  78. Michael J. Eager        Michael.Eager@eagercon.com
  79. Eager Consulting        (415) 325-8077 
  80. 1960 Park Boulevard, Palo Alto, CA 94306-1141
  81.  
  82.