home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!ieunet!nixeid!bsullivn
- From: bsullivn@nixeidsc.sni.ie (Bryan O'Sullivan)
- Newsgroups: comp.lang.c
- Subject: Re: While problem
- Message-ID: <131@nixeidsc.sni.ie>
- Date: 27 Jul 92 09:45:11 GMT
- References: <92204.160015GNR100@psuvm.psu.edu>
- Reply-To: bryan@sc.sni.ie
- Organization: Siemens-Nixdorf Software Development Centre, Dublin
- Lines: 65
- X-Newsreader: Tin 1.1 PL3
-
-
-
- GNR100@psuvm.psu.edu writes:
- :
- : I'm having trouble with a whil loop in function I am writing to
- : read a line from a fileby puttingeach character into an array, accesed by
- : pionters, until the '\n' character is reached of EOF is reached.
- : Trouble is, I keep getting an error message that the function needs an
- : Lvalue (address) inside the while loop. Any ideas as to the problem?
- : The filename is a global variable, and that's why it isn't declared.
-
- 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=0;
- :
- : while(((*string+i++=(char)getc(fname))!=EOF)
- : &&((*string+i++=(char)getc(fname))!='\n'));
- : return (*string+(i-1));
- : }
-
- char readline(char string[])
- {
- int i;
-
- for (i = 0; (string[i] = getc(fp)) != EOF; i++) {
- if (string[i] == '\n') {
- break;
- }
- }
- return &string[i-1];
- }
-
- That is probably what you really want to be doing. You're making a
- number of other errors in your own code; you're not dereferencing string
- properly (you have it declared as a vector of strings, not as a single
- string) -- this is the cause of your lvalue problem. If you got this to
- work, you would still have other griefs to contend with. Your
- comparisons in the while statement compare consecutive characters with
- EOF and EOL, so you could miss an EOF when checking for an EOL with your
- present code, and vice versa. It's not necessary to have those casts
- with getc() either; they just make your code unclear. Furthermore, the
- ugly pointer arithmetic you're doing is no longer really necessary;
- there was a time when compilers were bad at optimising array arithmetic,
- but most such compilers are (thankfully) no longer with us.
-
- Finally, I prefer the use of a for statement for the kind of job you're
- trying to do; it's just a stylistic issue, but it makles the code more
- clear. And finally finally, doing the return statement as I have done
- is also somewhat more clear, and won't cost anything if you have a
- half-decent compiler.
-
- My advice to you: read the sections on pointers in your C manual *very*
- carefully again. Good luck.
-
- -- Bryan
-
- --
- Bryan O'Sullivan +353-1-767551 x225
- Siemens-Nixdorf Informationssysteme AG bryan@sc.sni.ie
- Software Development Centre "Theory like mist on eyeglasses.
- Dublin Obscure facts." -- Charlie Chan
-