#Reading in files
2 messages in this thread
Trying to read information in from a file using fgetc(), I get all the
information into arrays, then the system crashes. There is more than enough
space in the arrays, but I'm getting a character displayed on my monitor that
looks like a "y" with an umlaute overtop. Sometimes I get a string of these
just before the system crashes. If I'm reading in data for a gradebook, say
lastname, firstname, p.o. box numberand grades is it best to try and read this
data in as all single int's with fgetc() using a while((y=fgetc(fp))!=EOF)
loop? George.
The best way of reading a file depends on how it was written. If you are
writing all your data as ASCII strings, and ending each record with a
newline you can use fgets to read a single record. This returns NULL if it
finds the end of the file before it gets a record.
> while((y=fgetc(fp))!=EOF)
{
/* Some code */
}
If y is an int this loop will carry on until you have read the entire
file. If y is a char I think it will try to keep reading past the end of
the file, and will probably write rubbish past the end of the array.
Peter Wade
Autopiloting from London, England