home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!unixhub!ditka!eagercon!eagercon!eager
- From: eager@eagercon.com (Michael J. Eager)
- Subject: Re: how to write C code so that redirection works
- Message-ID: <1993Jan11.061758.2317@eagercon.com>
- Sender: root@eagercon.com (Operator)
- Reply-To: eager@eagercon.com
- Organization: Eager Consulting
- References: <19456@mindlink.bc.ca>
- Date: Mon, 11 Jan 1993 06:17:58 GMT
- Lines: 50
-
- In article 19456@mindlink.bc.ca, a7657@mindlink.bc.ca (Stephen H. Kawamoto) writes:
- >i have a minor problem with C in that when i have a compiled file, FOO
- >FOO <infile >outfile
- >wont work. it 'hangs' and a ctrl-c or ctrl-break results in the outfile
- >having more bytes in it (stuff from the uninitialized memory space i think).
- >
- >it's the CTOPAS.C source, which turns Pascal into Pascal-like pseudocode (not
- >true Turbo Pascal in any case).
- >
- >here's the main where the problem might lie:
- >
- >>>
- >>> void main()
- >>> {
- >>> char c, *letter, word[100];
- >>> int wordlnth;
- >>>
- >>> letter=word;
- >>> wordlnth=0;
- >>> while((c=getchar()) != EOF) {
- >>> if(isalpha(c)) letter[wordlnth++]=c;
- >>> else {
- >>> if(wordlnth>0) { /* word ready to check
- >>> */
- >>> letter[wordlnth]='\0';
- >>> wtest(word); /* pass or replace it
- >>> */
- >>> wordlnth=0; /* reset index */
- >>> }
- >>> ctest(c); /* process following
- >>> character */
- >>> }
- >>> }
- >>> } /* Note: the last word in the file will be missed if it
- >>> followed by EOF with no intervening nonalphanumeric
- >>> characters. This is not a problem for Pascal or C source
- >>> files. */
-
-
- Try changing the declaration of c from char to int. Most implementations
- of getchar return a char sized value for the 256 character values which can fit
- in a byte, but the EOF value is outside this range (often -1). When you save
- it in char c, it gets truncated and is not recognized as the end of file.
-
-
- ---
- Michael J. Eager Michael.Eager@eagercon.com
- Eager Consulting (415) 325-8077
- 1960 Park Boulevard, Palo Alto, CA 94306-1141
-
-