Re: fscanf hangs
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...
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iR=
ows-1;
int iRead;
FILE *fpInFile;
if ((fpInFile= fopen(csFileName, "r")) == NULL) ret=
urn
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 {
// ...
}
}
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?
--
James Kanze (GABI Software, from CAI) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34