home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19524 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.1 KB  |  63 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!unixhub!ditka!eagercon!eagercon!eager
  3. From: eager@eagercon.com (Michael J. Eager)
  4. Subject: Re: how to write C code so that redirection works
  5. Message-ID: <1993Jan11.061758.2317@eagercon.com>
  6. Sender: root@eagercon.com (Operator)
  7. Reply-To: eager@eagercon.com
  8. Organization: Eager Consulting
  9. References: <19456@mindlink.bc.ca>
  10. Date: Mon, 11 Jan 1993 06:17:58 GMT
  11. Lines: 50
  12.  
  13. In article 19456@mindlink.bc.ca, a7657@mindlink.bc.ca (Stephen H. Kawamoto) writes:
  14. >i have a minor problem with C in that when i have a compiled file, FOO
  15. >FOO <infile >outfile
  16. >wont work. it 'hangs' and a ctrl-c or ctrl-break results in the outfile
  17. >having more bytes in it (stuff from the uninitialized memory space i think).
  18. >
  19. >it's the CTOPAS.C source, which turns Pascal into Pascal-like pseudocode (not
  20. >true Turbo Pascal in any case).
  21. >
  22. >here's the main where the problem might lie:
  23. >
  24. >>>
  25. >>> void main()
  26. >>> {
  27. >>>    char c, *letter, word[100];
  28. >>>    int wordlnth;
  29. >>>
  30. >>>    letter=word;
  31. >>>    wordlnth=0;
  32. >>>    while((c=getchar()) != EOF) {
  33. >>>       if(isalpha(c)) letter[wordlnth++]=c;
  34. >>>       else {
  35. >>>          if(wordlnth>0) {            /* word ready to check
  36. >>> */
  37. >>>             letter[wordlnth]='\0';
  38. >>>             wtest(word);             /* pass or replace it
  39. >>> */
  40. >>>             wordlnth=0;              /* reset index */
  41. >>>          }
  42. >>>          ctest(c);                   /* process following
  43. >>> character */
  44. >>>       }
  45. >>>    }
  46. >>> }   /* Note:  the last word in the file will be missed if it
  47. >>> followed by EOF with no intervening nonalphanumeric
  48. >>> characters.  This is not a problem for Pascal or C source
  49. >>> files.  */
  50.  
  51.  
  52. Try changing the declaration of c from char to int.  Most implementations
  53. of getchar return a char sized value for the 256 character values which can fit
  54. in a byte, but the EOF value is outside this range (often -1).  When you save
  55. it in char c, it gets truncated and is not recognized as the end of file.
  56.  
  57.  
  58. ---
  59. Michael J. Eager        Michael.Eager@eagercon.com
  60. Eager Consulting        (415) 325-8077 
  61. 1960 Park Boulevard, Palo Alto, CA 94306-1141
  62.  
  63.