home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!gatech!taco!SOMERVILLE@BAEPV3.NCSU.EDU
- From: somerville@BAEPV3.NCSU.EDU (SOMERVILLE)
- Subject: Re: While problem
- Message-ID: <0095E316.55D07C00@BAEPV3.NCSU.EDU>
- Sender: news@ncsu.edu (USENET News System)
- Reply-To: somerville@BAEPV3.NCSU.EDU (SOMERVILLE)
- Organization: North Carolina State University
- References: <92204.160015GNR100@psuvm.psu.edu>,<131@nixeidsc.sni.ie>
- Date: Mon, 27 Jul 1992 13:20:59 GMT
- Lines: 63
-
- In article <131@nixeidsc.sni.ie>, bsullivn@nixeidsc.sni.ie (Bryan O'Sullivan) writes:
- >
- >
- >GNR100@psuvm.psu.edu writes:
-
- [Stuff Deleted]
-
- >: char readline(char *string[])
- >: {
- >: int i=0;
- >:
- >: while(((*string+i++=(char)getc(fname))!=EOF)
- >: &&((*string+i++=(char)getc(fname))!='\n'));
- >: return (*string+(i-1));
- >: }
- >
- >Well, first off, your call to getc will bomb if you ever get the rest of
- >the program running. Getc(3) takes a FILE pointer, not a file name, as
- >argument. Also, (no flame intended) you're making the classic new C
- >programmer mistake of trying to do too many "neat" things inside your
- >while loop, which is part of the reason why it isn't working.
- >
- >
- >char readline(char string[])
- >{
- > int i;
- >
- > for (i = 0; (string[i] = getc(fp)) != EOF; i++) {
- > if (string[i] == '\n') {
- > break;
- > }
- > }
- > return &string[i-1];
- >}
- >
- >
- >[Rest Deleted]
-
- Although this may work, I do not believe it is portable.
- If string is a char *, then
-
- (string[i] = getc( fp )) != EOF
-
- could be a problem. getc() returns an int and EOF is a
- negative value. You do not know if the implementation treats chars
- as signed or unsigned. If you want to test for EOF, use an int to
- hold the return value from getc():
-
- int c;
- .
- .
- ... (c = getc( fp )) != EOF ...
- .
- .
- string[i] = c;
-
-
- I could certainly be wrong -- perhaps someone with a copy of the
- standard could comment on this.
-
- --
- Garth Somerville
- somerville@bae.ncsu.edu
-