hey all,
did c a while back, trying to get back into it. the following stub of code fails on the while(!feof(fp)) for some reason. could someone help? Thanks
Please note that all variables here are indeed declared:
Keep in mind that if you do not know why something is failing, you can't
be sure what's relevant to the fact that it's failing. You should
provide a complete compilable program demonstrating your problem, not
just a few lines of code. The code where you actually observed the
failure is probably far too complicated, so you should simplify the code
as much as possible before showing it to us, removing any parts that
leave the problem intact. But after you've simplified it, it should
still be a complete compilable program - confirm this by compiling it
before you post it. It must still demonstrate the same problem you're
asking about - also confirm this before posting your message.
For a program like the following that does I/O, it's essential that you
give us an example of inputs that actually trigger the problem.
You make no attempt to check whether fgets() succeeded. If it fails due
to an I/O error, the contents of 'line' are indeterminate. If it reads
the last line of the file, the end-of-file status will not be set until
until the next call to fgets(), and that call will leave the contents of
'line' unchanged, so your program will process the last line of code twice.
tmpNode = (struct dlnode*)malloc(sizeof(struct dlnode));
if(tmpNode == NULL)
{
perror("unable to allocate memory");
intResult= -2;
}
You check whether tmpNode is null - that's good. You produce an
appropriate message - that's good. Then you continue processing with the
null value of tmpNode. That's very bad. Since you didn't see that
message, this probably isn't the problem your seeing, but it is the
wrong way to use malloc().
sscanf(line,"%d:%s",&intVal,strVal,100);
You make no attempt to determine whether sscanf() succeeded. It could
fail if the line is not of the specified form, in which case the
contents of intVal and strVal will be indeterminate.
If tmpNode is null, each of next two line, will have undefined behavior.
Depending upon what addDoubleNode() does with it's argument, the
behavior of that function is likely to also be undefined.
strcpy(tmpNode->chrName,strVal);
tmpNode->intID = intVal;
if(addDoubleNode(tmpNode)<0)
{
perror("unable to create new node...error code (-3)");
exit(-3);
}
}while(!feof(fp)); //On this line the code fails.
In C standard I/O, the end of a file is detected after a failed attempt
to read it, therefore, it's more appropriate to use an overall structure
like the following:
while(fgets(line, 80, fp) == line)
{
// Process line.
}
if(ferror(fp))
{
// handle I/O error
}
else
{
// End of file reached - if any special.
// handling is needed, put it here
}
While I have found many problems with your program, I can't be sure
whether any of them are relevant to the failure you observed. You should
identify what inputs were given to the program, what you expected to
happen when you gave them, and what actually happened. "the code fails"
is very nearly useless information, without details about what you mean
by "fails".