-
fscanf woes
I'm forced to use plain C and i'm having problems with fscanf....
I'm simply reading in lines from a text file but the last line may
or may not have a \n and the following piece of code gives
an exception at the feof test line (i hate using while).
Any advice on this would be greatly appreciated.
Code:
float fData[11];
while(!feof(m_pFile)
{
for(int i = 0; i< 12; i++)
fscanf(m_pFile, "%f", &fData[i]);
}
-
You need to test for EOF differently (I think):
Code:
while(fscanf(m_pFile, "%f", &fData[i]) != EOF)
{
// Do whatever
}
If you don't like that you could always use fgets():
Code:
while(!feof(m_pFile))
{
char szBuff[BUFF_SIZE]; // Depends on how long the lines are
fgets(szBuff, 10, fPtr); // Get 10 chars from file - or till end of line
if(ferror(m_pFile))
{
// Handle the error
}
else
{
// Convert the character array to a float
}
}
Or something like that. This allows you to test for errors. Otherwise just use the first example - if it doesn't work, come back and explain the errors.
HD
-
What's so plain about C?! :(
Using a space in the format string will make the scanning function skip several types of whitespace characters:
Code:
fscanf(m_pFile, " %f", &fData[i])
-
"Plain" C as opposed to C"++"
-
In that case, it's just "C" or "C++".