fscanf hangs | | |
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.
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);
if (iRead==0 || iRead==EOF) break;
} 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.
Many thanks in advance for any assistance,
Peter. | | | | re: fscanf hangs
On Jun 18, 10:19 am, PeterOut <MajorSetb...@excite.comwrote: Quote:
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.
>
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);
if (iRead==0 || iRead==EOF) break;
} 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.
>
Many thanks in advance for any assistance,
Peter.
Dear Peter,
Your are not closing the file.
Use fclose for closing the file.
Thanks and regards,
Amal P. | | | | re: fscanf hangs
On Jun 17, 9:32 pm, Amal P <enjoyam...@gmail.comwrote: Quote:
On Jun 18, 10:19 am, PeterOut <MajorSetb...@excite.comwrote:
>
>
>
>
> Quote:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
> Quote:
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.
> Quote:
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
int iRead;
FILE *fpInFile;
> Quote:
if ((fpInFile= fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);
> Quote:
do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
if (iRead==0 || iRead==EOF) break;
} while (lX < lLastX || lY < lLastY);
> > Quote:
I am wondering if I should just do binary reading so as to have more
control over what is going on.
> Quote:
Many thanks in advance for any assistance,
Peter.
>
Dear Peter,
>
Your are not closing the file.
Use fclose for closing the file.
>
Thanks and regards,
Amal P.- Hide quoted text -
>
- Show quoted text -
Sorry. I am closing the file with
fclose(fpInFile);
but forgot to include that in the sample code that I posted. The
problem, unfotruntately, lies elsewhere.
Thanks,
Peter. | | | | re: fscanf hangs
"PeterOut" <MajorSetback@excite.comwrote in message
news:1182129577.310563.191160@c77g2000hse.googlegr oups.com... Quote:
>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.
>
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);
%d is for type 'int'. For type 'long', use %ld.
Also make sure you've #included <stdio.hfor the
declaration of 'fscanf()'.
-Mike | | | | re: fscanf hangs
PeterOut wrote: Quote:
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
from the variable names, it looks like a Transylvanian Heresy problem,
but... Quote:
but fscanf has been
randomly hanging on me.
Because you use the wrong specifiers to fscanf
[...] Quote:
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
[...] Quote:
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
"%d" is the specifier for an int, but lLong1, lLong2, and lNum are not
ints. If you are going to clutter your code with those god-awful names
that carry their types with them, I would think you would learn basic
stuff like this. | | | | re: fscanf hangs
On Jun 18, 3:19 am, PeterOut <MajorSetb...@excite.comwrote: Quote:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
Quote:
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... Quote:
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
int iRead;
FILE *fpInFile;
Quote:
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. Quote:
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 {
// ...
}
} Quote:
>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. Quote:
} while (lX < lLastX || lY < lLastY);
}
Quote:
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ée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 | | | | re: fscanf hangs
Martin Ambuhl wrote: .... snip ... Quote:
>
"%d" is the specifier for an int, but lLong1, lLong2, and lNum
are not ints. If you are going to clutter your code with those
god-awful names that carry their types with them, I would think
you would learn basic stuff like this.
That seems like fairly accurate straight forward advice :-)
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com | | | | re: fscanf hangs
On Jun 17, 10:06 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote: Quote:
"PeterOut" <MajorSetb...@excite.comwrote in message
>
news:1182129577.310563.191160@c77g2000hse.googlegr oups.com...
>
>
>
>
> Quote:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
> Quote:
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.
> Quote:
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
int iRead;
FILE *fpInFile;
> Quote:
if ((fpInFile= fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);
> Quote:
do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
>
%d is for type 'int'. For type 'long', use %ld.
>
Also make sure you've #included <stdio.hfor the
declaration of 'fscanf()'.
>
-Mike- Hide quoted text -
>
- Show quoted text -
Thanks very much for your help.
I had #included <stdio.hbut changed the %d to $ld and have not had a
problem since. I was surprised that it made a difference on 32-bit
Windows since I thought int and long were both 32 bits on 32-bit
Windows. I think they are different on Unix and Linux where I think
it is 32 bits for int and 64 bits for long but I may be wrong.
Thanks again,
Peter. | | | | re: fscanf hangs
On Jun 18, 12:58 am, Martin Ambuhl <mamb...@earthlink.netwrote: Quote:
PeterOut wrote: Quote:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
> Quote:
I am not sure if this is a C, C++ or MS issue
>
from the variable names, it looks like a Transylvanian Heresy problem,
The original variable names were application specific althought they
included the Hungarian notation that everyone seems to like so much ;) Quote:
but...
> Quote:
but fscanf has been
randomly hanging on me.
>
Because you use the wrong specifiers to fscanf
>
[...]
>
>
> Quote:
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
[...] Quote:
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
>
"%d" is the specifier for an int, but lLong1, lLong2, and lNum are not
ints. If you are going to clutter your code with those god-awful names
that carry their types with them, I would think you would learn basic
stuff like this.
Mike had pointed that out and I have not had any problems since I
fixed it. However, it had been runing w/o problems beforehand and
then started playing up and hagning there for some reason.
Thanks,
Peter. | | | | re: fscanf hangs
On Jun 18, 4:36 am, James Kanze <james.ka...@gmail.comwrote: Quote:
On Jun 18, 3:19 am, PeterOut <MajorSetb...@excite.comwrote:
> Quote:
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. Quote:
> Quote:
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);
> Quote:
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.
> Quote:
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. Quote:
> Quote:
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.
> Quote:
} 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. | | | | re: fscanf hangs
PeterOut wrote: Quote:
On Jun 18, 12:58 am, Martin Ambuhl <mamb...@earthlink.netwrote: Quote:
>PeterOut wrote: Quote:
>>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
>from the variable names, it looks like a Transylvanian Heresy problem,
>
The original variable names were application specific althought they
included the Hungarian notation that everyone seems to like so much ;)
Not me | | | | re: fscanf hangs
On Jun 17, 10:06 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote: Quote:
"PeterOut" <MajorSetb...@excite.comwrote in message
>
news:1182129577.310563.191160@c77g2000hse.googlegr oups.com...
>
>
>
>
> Quote:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
> Quote:
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.
> Quote:
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
int iRead;
FILE *fpInFile;
> Quote:
if ((fpInFile= fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);
> Quote:
do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
>
%d is for type 'int'. For type 'long', use %ld.
>
Also make sure you've #included <stdio.hfor the
declaration of 'fscanf()'.
>
-Mike- Hide quoted text -
>
- Show quoted text -
Unfortunately, the problem has started happening again, even with %ld
and #include <stdio.h>. I wonder if it's a problem with MS Visual C+
+. It seems to be correlated with my "breaking" during operation
although it is not clear whther that is a cause or an effect.
Thanks,
Peter. | | | | re: fscanf hangs
PeterOut wrote: Quote:
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.
>
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);
if (iRead==0 || iRead==EOF) break;
} 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.
>
Many thanks in advance for any assistance,
Peter.
>
Hmmm, I was thinking that it might be a consequence of corrupt memory
(like buffer overruns, changing memory after free() (or equivalent) or
some other similar problem).
Test if you have access to a function called AfxCheckMemory(). (It is
available in later versions of their compiler, don't know if it is in
that version however).
If it is available try placing
ASSERT(AfxCheckMemory());
immediately before the call to fscanf(). If it breaks you have
previously overwritten some memory somewhere in your program where that
should not happen... | | | | re: fscanf hangs
On Jun 23, 2:50 pm, Johan Bengtsson <qwerty...@hotmail.comwrote: Quote:
PeterOut wrote: Quote:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
> Quote:
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.
> Quote:
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
int iRead;
FILE *fpInFile;
> Quote:
if ((fpInFile= fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);
> Quote:
do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
if (iRead==0 || iRead==EOF) break;
} while (lX < lLastX || lY < lLastY);
}
> Quote:
I am wondering if I should just do binary reading so as to have more
control over what is going on.
> Quote:
Many thanks in advance for any assistance,
Peter.
>
Hmmm, I was thinking that it might be a consequence of corrupt memory
(like buffer overruns, changing memory after free() (or equivalent) or
some other similar problem).
Test if you have access to a function called AfxCheckMemory(). (It is
available in later versions of their compiler, don't know if it is in
that version however).
If it is available try placing
>
ASSERT(AfxCheckMemory());
>
immediately before the call to fscanf(). If it breaks you have
previously overwritten some memory somewhere in your program where that
should not happen...- Hide quoted text -
>
- Show quoted text -
Hi Johan,
That looks likfe a handy function. Thanks for bringing it to my
attention. I put it in and it didn't flag anything. I have decided
to give up on fscanf and add the following code instead.
ERROR_NUMBER FReadLine(FILE *fpInputFile, char *csBuffer)
{
int i;
if ((csBuffer[0] = fgetc( fpInputFile )) == EOF) return
ERROR_READING_FILE;
i = (csBuffer[0] == '\n')? 0 : 1;
while ((csBuffer[i] = fgetc( fpInputFile )) != '\n')
{
if (csBuffer[i++] == EOF) return ERROR_READING_FILE;
}
csBuffer[i] = '\0';
return ERROR_NONE;
}
if ((fpFile = fopen(csFileName, "r")) == NULL)
{
// Error handling
}
{
float fMean;
long lX, lY, lLastX = fppPlane->iColumns-1, lLastY = fppPlane- char csBuffer[32];
do
{
if ((enErrorNumber=FReadLine(fpFile, csBuffer))!=ERROR_NONE) break;
lX=atoi(strtok(csBuffer, "\t"));
lY=atoi(strtok(NULL, "\t"));
fMean=(float)atof(strtok(NULL, "\t"));
} while (lX < lLastX || lY < lLastY);
}
fclose(fpFile);
So far no problem.
Thanks again for your help,
Peter. |  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,585 network members.
|