home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!gatech!asuvax!ennews!envmsa.eas.asu.edu!ptran
- From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
- Subject: Re: While problem
- Message-ID: <22JUL199216531439@envmsa.eas.asu.edu>
- News-Software: VAX/VMS VNEWS 1.4-b1
- Sender: news@ennews.eas.asu.edu (USENET News System)
- Organization: Arizona State University, Tempe, AZ
- References: <92204.160015GNR100@psuvm.psu.edu>
- Date: Wed, 22 Jul 1992 23:53:00 GMT
- Lines: 65
-
- In article <92204.160015GNR100@psuvm.psu.edu>, 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.
-
- >char readline(char *string[])
- >{
- > int i=0;
- >
- > while(((*string+i++=(char)getc(fname))!=EOF)
- > &&((*string+i++=(char)getc(fname))!='\n'));
- > return (*string+(i-1));
- >}
-
- I commend your courage for even attempting to write this kind of
- gobbledegook -- I don't think even B. Kernighan or D. Ritchie would even
- try this stuff, let alone P.J. Plauger.
-
- Here are my observations:
-
- 1) Function getc accepts a file pointer ("FILE *"), *NOT* a file name. You
- can find this out in *ANY* book on C -- just look for a description of the
- routine.
-
- 2) You are doing too many things at once to even possibly debug reliably.
- Do you really understand all those operators, their precedence, their
- side-effects, etc. ad inifitum. et. al.
-
- 3) To read in a line of text, you do not need an array of pointers (that's
- what "char *string[]" is and means). You just need an array of characters
- into which a file I/O routine will fill with the characters.
-
- 4) Why are you performing arithmetic operations on the left side of an
- assignment statement? Look in a C book on making assignments.
-
- If all you want to do is read in a string, then there are many
- standard library functions you can call: fgets, gets, fscanf, scanf ...
- They are declared in the header file "stdio.h" that you include in a module
- when you use them.
-
- #define MAX_INPUT_CHARS 132 /* Or some relevant value. */
-
- ...
-
- char szData[MAX_INPUT_CHARS];
- FILE *pfileInput; /* Pointer to a file for input. */
-
- ...
-
- if (fgets( szData, sizeof( szData ), pfileInput) == NULL)
- {
- /* An error occurred. */
- }
-
- Above all ... LOOK IT UP IN A C BOOK. Only post when you have
- EXHAUSTED your resources for information. Have you looked this problem up?
-
- ---
- Phi-Long Tran
- ptran@asuvax.eas.asu.edu
- Arizona State University
-