Re: fscanf hangs
On Jun 18, 4:36 am, James Kanze <james.ka...@gmail.com> wrote:
On Jun 18, 3:19 am, PeterOut <MajorSetb...@excite.com> wrote:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
I am not sure if this is a C, C++ or MS issue but fscanf has been
randomly hanging on me. I make the call hundreds, if not thousands,
of times but it hangs in different places with the same data. The
offending code follows.
The code would be a lot easier to understand (and a lot safer)
if you'd use ifstream instead of fscanf. However...
Thanks. The code appears to be woking OK now that I have fixed the %d/
%ld problem but will certainly look into that if fscanf gives me any
more problems.
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
int iRead;
FILE *fpInFile;
if ((fpInFile= fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);
do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
As others have pointed out, you need %ld to be correct. If this
doesn't cause problems immediately, it's because by pure chance,
longs and ints have the same size on your implementation.
if (iRead==0 || iRead==EOF) break;
And what is fscanf supposed to return in the case of:
"1 2 3x 4.5 6"
?
Personally, I'd probably impose that each data set be on a
separate line, and do something like:
std::string line ;
int lineNo = 0 ;
while ( std::getline( input, line ) ) {
++ lineNo ;
std::istringstream s( line ) ;
s >> long1 >> long2 >> float1 >> float2 >> num >> std::ws ;
if ( ! s || s.get() != EOF ) {
std::cerr << "Syntax error in line "
<< lineNo
<< ", ignoring it"
<< std::endl ;
} else {
// ...
}
}
Yes. Being able to split things up would probably make it easier to
diagnose the problem.
From your code, it's also not too clear where the lX and lY come
from, or how they evolve to handle your end conditions.
Normally, written correctly, using std::vector, you should be
able to automatically scale any arrays to the amount of data
read.
} while (lX < lLastX || lY < lLastY);
}
I am wondering if I should just do binary reading so as to have more
control over what is going on.
How would that change anything?
I thought that it might make the problems more transparent. fscanf
did not return an error. It just hung ad infinitum. Something
binary, I could just read the bytes and it would not really matter
about formatting. I could read everythings in as a character string
and then analyze it rather than relying upon however fscanf, or
whatever, was written.
Thanks again for your help,
Peter.