By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,613 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,613 IT Pros & Developers. It's quick & easy.

fscanf hangs

P: n/a
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.

Jun 18 '07 #1
Share this Question
Share on Google+
37 Replies


P: n/a
On Jun 18, 10:19 am, PeterOut <MajorSetb...@excite.comwrote:
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.

Jun 18 '07 #2

P: n/a
On Jun 17, 9:32 pm, Amal P <enjoyam...@gmail.comwrote:
On Jun 18, 10:19 am, PeterOut <MajorSetb...@excite.comwrote:


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.- 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.

Jun 18 '07 #3

P: n/a

"PeterOut" <Ma**********@excite.comwrote in message
news:11**********************@c77g2000hse.googlegr oups.com...
>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

Jun 18 '07 #4

P: n/a
On Sun, 17 Jun 2007 18:19:37 -0700, PeterOut <Ma**********@excite.com>
wrote:
>I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2).
You posted to numerous inconsistent newsgroups. Are you coding in C
or C++? What do you think this has to do with Microsoft Foundation
Classes?
>
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
How do you determine that?
> iRead=fscanf(fpInFile, "%d%d%f%f%d", &lLong1, &lLong2, &fFloat1,
&fFloat2, &lNum);
if (iRead==0 || iRead==EOF) break;
You really should check that fscanf did everything you asked and iRead
is in fact 5. If 0<=iRead<5, there is something wrong with the data
in your file.
> } 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.

Remove del for email
Jun 18 '07 #5

P: n/a
PeterOut 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
from the variable names, it looks like a Transylvanian Heresy problem,
but...
but fscanf has been
randomly hanging on me.
Because you use the wrong specifiers to fscanf

[...]
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
[...]
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.
Jun 18 '07 #6

P: n/a
On Jun 18, 3:19 am, PeterOut <MajorSetb...@excite.comwrote:
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 =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 {
// ...
}
}
>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:ja*********@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

Jun 18 '07 #7

P: n/a
Martin Ambuhl wrote:
>
.... snip ...
>
"%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

Jun 19 '07 #8

P: n/a
On Jun 17, 10:06 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"PeterOut" <MajorSetb...@excite.comwrote in message

news:11**********************@c77g2000hse.googlegr oups.com...


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- 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.

Jun 19 '07 #9

P: n/a
On Jun 18, 12:58 am, Martin Ambuhl <mamb...@earthlink.netwrote:
PeterOut 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

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 ;)
but...
but fscanf has been
randomly hanging on me.

Because you use the wrong specifiers to fscanf

[...]
float fFloat1, fFloat2;
long lLong1, lLong2, lNum, lLastX = iColumns-1, lLastY =iRows-1;
[...]
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.

Jun 19 '07 #10

P: n/a
On Jun 18, 4:36 am, James Kanze <james.ka...@gmail.comwrote:
On Jun 18, 3:19 am, PeterOut <MajorSetb...@excite.comwrote:
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.
Jun 19 '07 #11

P: n/a
PeterOut wrote:
On Jun 18, 12:58 am, Martin Ambuhl <mamb...@earthlink.netwrote:
>PeterOut 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
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
Jun 21 '07 #12

P: n/a
"Johan Bengtsson" writes:
PeterOut wrote:
>On Jun 18, 12:58 am, Martin Ambuhl <mamb...@earthlink.netwrote:
>>PeterOut 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
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
You're supposed to read the smiley to indicate negation. Smileys are so
overused one needs a monograph on how to interpret them.
Jun 21 '07 #13

P: n/a
On Jun 17, 10:06 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"PeterOut" <MajorSetb...@excite.comwrote in message

news:11**********************@c77g2000hse.googlegr oups.com...


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- 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.

Jun 22 '07 #14

P: n/a
PeterOut <Ma**********@excite.comwrites:
On Jun 17, 10:06 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"PeterOut" <MajorSetb...@excite.comwrote in message
[...]
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.
[offending code snipped]
>%d is for type 'int'. For type 'long', use %ld.

Also make sure you've #included <stdio.hfor the
declaration of 'fscanf()'.

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.
If you'll post your current code, along with a detailed description of
how it's failing, we can probably help. We can't guess what your code
actually looks like now. In the meantime, read section 12 of the
comp.lang.c FAQ, <http://www.c-faq.com/>; it's entirely possible that
you'll find the solution there.

I've dropped comp.lang.c++ and comp.os.ms-windows.programmer.tools.misc
from the newsgroups line.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 22 '07 #15

P: n/a
On Fri, 22 Jun 2007 07:55:45 -0700, PeterOut
<Ma**********@excite.comwrote:
snip obsolete code
>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.
If you don't post your current code, no one will be able to help you.
If you keep posting to irrelevant groups, no one will want to help
you.
Remove del for email
Jun 22 '07 #16

P: n/a
On Jun 22, 3:12 pm, Keith Thompson <k...@mib.orgwrote:
PeterOut <MajorSetb...@excite.comwrites:
On Jun 17, 10:06 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"PeterOut" <MajorSetb...@excite.comwrote in message
[...]
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.

[offending code snipped]
%d is for type 'int'. For type 'long', use %ld.
Also make sure you've #included <stdio.hfor the
declaration of 'fscanf()'.
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.

If you'll post your current code, along with a detailed description of
how it's failing, we can probably help. We can't guess what your code
actually looks like now. In the meantime, read section 12 of the
comp.lang.c FAQ, <http://www.c-faq.com/>; it's entirely possible that
you'll find the solution there.
Sorry about that. Herewith is the updated code.

------------------------------------------------------------

if ((fpFile = fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);

{
float fMean, fSD;
long lX, lY, lNumItems, lLastX = fppPlane->iColumns-1, lLastY =
fppPlane->iRows-1;
int iRead;

do
{
iRead=fscanf(fpFile, "%ld%ld%f%f%ld", &lX, &lY, &fMean, &fSD,
&lNumItems);
if (iRead==0 || iRead==EOF) break;
fppPlane->Data[lY][lX] = fMean;
} while (lX < lLastX || lY < lLastY);
}

fclose(fpFile);

------------------------------------------------------------

Usually there is no problem but sometimes it hangs on
iRead=fscanf(fpFile, "%ld%ld%f%f%ld", &lX, &lY, &fMean, &fSD,
&lNumItems);

When it does not, iRead==5.

Thanks,
Peter.

Jun 23 '07 #17

P: n/a
PeterOut <Ma**********@excite.comwrites:
On Jun 22, 3:12 pm, Keith Thompson <k...@mib.orgwrote:
[...]
>If you'll post your current code, along with a detailed description of
how it's failing, we can probably help. We can't guess what your code
actually looks like now. In the meantime, read section 12 of the
comp.lang.c FAQ, <http://www.c-faq.com/>; it's entirely possible that
you'll find the solution there.

Sorry about that. Herewith is the updated code.

------------------------------------------------------------

if ((fpFile = fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);

{
float fMean, fSD;
long lX, lY, lNumItems, lLastX = fppPlane->iColumns-1, lLastY =
fppPlane->iRows-1;
int iRead;

do
{
iRead=fscanf(fpFile, "%ld%ld%f%f%ld", &lX, &lY, &fMean, &fSD,
&lNumItems);
if (iRead==0 || iRead==EOF) break;
fppPlane->Data[lY][lX] = fMean;
} while (lX < lLastX || lY < lLastY);
}

fclose(fpFile);

------------------------------------------------------------

Usually there is no problem but sometimes it hangs on
iRead=fscanf(fpFile, "%ld%ld%f%f%ld", &lX, &lY, &fMean, &fSD,
&lNumItems);

When it does not, iRead==5.
It's still hard to tell what's going on, because you haven't shown us
a complete program. I see no main function, and no declarations
for fpFile, csFileName, ErrorOpeningFile, etc.

You also haven't given us any clue about what information is in the
file you're attempting to read.

Remember that fscanf with any of the numeric formats, including "%ld"
and "%f", will skip over any leading whitespace before reading a
number; whitespace can include newline characters. Are you attempting
to read from a disk file? Could it be that you're expecting a certain
layout for each input line, and the actual input you're seeing doesn't
match your expectation? I wouldn't expect fscanf to "hang" unless
it's reading from an interactive device, but it's something to think
about.

Also, fscanf invokes undefined behavior if the input value overflows
the target type. For example, if you attempt to read the string
"1.0e1000000" with a "%f" format, *anything* could happen.

Take the code you just posted and modify it into *small* *complete*
program that exhibits the problem. Here's a suggestion; tweak to
taste:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
FILE *fpFile;
const char *csFileName = "input.txt"; /* or whatever */

if ((fpFile = fopen(csFileName, "r")) == NULL) {
fprintf(stderr, "fopen failed for %s\n", csFileName);
exit(EXIT_FAILURE);
}

{
int iRead;
long input1, input2;
float input3, input4;
long input5;

while (1) {
iRead=fscanf(fpFile,
"%ld%ld%f%f%ld",
&input1, &input2, &input3, &input4, &input5);
printf("%d: %ld %ld %g %g %ld\n",
iRead, input1, input2, input3, input4, input5);
if (iRead < 5) {
break;
}
}
}
fclose(fpFile);
exit(EXIT_SUCCESS);
}

Note that this just reads input items; it doesn't know or care what
they mean or how they fit into your other data structures.

If you can reproduce the same problem with this program, or with
something similar, then post again -- and tell us exactly what the
input file looks like. (In fact, trim the input file itself down to
the minimum necessary to reproduce the problem.) If you can do this,
you're likely to figure out the problem for yourself before you get
around to posting.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 23 '07 #18

P: n/a
On Fri, 22 Jun 2007 19:09:42 -0700, PeterOut <Ma**********@excite.com>
wrote:
>Sorry about that. Herewith is the updated code.

------------------------------------------------------------

if ((fpFile = fopen(csFileName, "r")) == NULL) return
ErrorOpeningFile(csFileName);

{
float fMean, fSD;
long lX, lY, lNumItems, lLastX = fppPlane->iColumns-1, lLastY =
fppPlane->iRows-1;
int iRead;

do
{
iRead=fscanf(fpFile, "%ld%ld%f%f%ld", &lX, &lY, &fMean, &fSD,
&lNumItems);
if (iRead==0 || iRead==EOF) break;
What you want here is
if (iRead != 5){Some very thorough debugging output. Printout
iRead and all five objects that should have been updated. You can end
with a break if you like but give yourself a fighting chance to see
where the error is.}
> fppPlane->Data[lY][lX] = fMean;
At this point, you might want to include some output just so you can
see the program progress through the file.
> } while (lX < lLastX || lY < lLastY);
This implies it is possible for lX to exceed lLastX. If it exceeds
the limit by more than one you will have invoked undefined behavior in
the previous assignment statement. Ditto for lY. It also means you
stop processing after the first value is stored in the last row or
last column, probably not what you intended.
>}

fclose(fpFile);

------------------------------------------------------------

Usually there is no problem but sometimes it hangs on
iRead=fscanf(fpFile, "%ld%ld%f%f%ld", &lX, &lY, &fMean, &fSD,
&lNumItems);
How do you know this is where it hangs?
>
When it does not, iRead==5.

Remove del for email
Jun 23 '07 #19

P: n/a
PeterOut 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.

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...

Jun 23 '07 #20

P: n/a
On Jun 22, 7:28 pm, Barry Schwarz <schwa...@doezl.netwrote:
If you keep posting to irrelevant groups, no one will want to help
you.
The problem is that I am not sure if it a problem with my C code or a
problem with MS Visual C++.

Jun 24 '07 #21

P: n/a
PeterOut wrote:
Barry Schwarz <schwa...@doezl.netwrote:
>If you keep posting to irrelevant groups, no one will want to
help you.

The problem is that I am not sure if it a problem with my C code
or a problem with MS Visual C++.
Look at line 42.

--
<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

Jun 25 '07 #22

P: n/a
On Sun, 24 Jun 2007 07:04:37 -0700, PeterOut <Ma**********@excite.com>
wrote:
>On Jun 22, 7:28 pm, Barry Schwarz <schwa...@doezl.netwrote:
>If you keep posting to irrelevant groups, no one will want to help
you.

The problem is that I am not sure if it a problem with my C code or a
problem with MS Visual C++.
Now you are just being disingenuous. In spite of the product name,
your code was C, not C++. The product contains both compilers. But
you already new that since you were compiling C code. And you
certainly didn't invoke any tools. That eliminates two of the
extraneous newsgroups. And since you already know there is a
collection of Microsoft newsgroups, you could have found the one that
deals with the Microsoft version of the language pretty easily.
Remove del for email
Jun 25 '07 #23

P: n/a
On Jun 23, 2:50 pm, Johan Bengtsson <qwerty...@hotmail.comwrote:
PeterOut 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.
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...- 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-
>iRows-1;
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.

Jun 26 '07 #24

P: n/a
On Jun 24, 11:10 am, CBFalconer <cbfalco...@yahoo.comwrote:
PeterOut wrote:
Barry Schwarz <schwa...@doezl.netwrote:
If you keep posting to irrelevant groups, no one will want to
help you.
The problem is that I am not sure if it a problem with my C code
or a problem with MS Visual C++.

Look at line 42.
Of what?

Jun 26 '07 #25

P: n/a
On Mon, 25 Jun 2007 19:15:44 -0700, PeterOut <Ma**********@excite.com>
wrote:

snip obsolete code that should not have been quoted
>
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;
}
This sure seems like a lot of work just avoid calling fgets.
>
if ((fpFile = fopen(csFileName, "r")) == NULL)
{
// Error handling
}
{
float fMean;
long lX, lY, lLastX = fppPlane->iColumns-1, lLastY = fppPlane-
>>iRows-1;
char csBuffer[32];
Is 32 bytes really sufficient to hold the longest line in your file?
>
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"));
The cast is superfluous.
> } while (lX < lLastX || lY < lLastY);
}
fclose(fpFile);

So far no problem.
So you have managed to camouflage the problem with no understanding of
what the real issue was?
>
Thanks again for your help,
Peter.

Remove del for email
Jun 26 '07 #26

P: n/a
PeterOut said:
On Jun 24, 11:10 am, CBFalconer <cbfalco...@yahoo.comwrote:
>PeterOut wrote:
Barry Schwarz <schwa...@doezl.netwrote:
>If you keep posting to irrelevant groups, no one will want to
help you.
The problem is that I am not sure if it a problem with my C code
or a problem with MS Visual C++.

Look at line 42.
Of what?
Precisely his point, I think.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 26 '07 #27

P: n/a
On Mon, 25 Jun 2007 19:18:21 -0700, in comp.lang.c , PeterOut
<Ma**********@excite.comwrote:
>On Jun 24, 11:10 am, CBFalconer <cbfalco...@yahoo.comwrote:
>PeterOut wrote:
Barry Schwarz <schwa...@doezl.netwrote:
>If you keep posting to irrelevant groups, no one will want to
help you.
The problem is that I am not sure if it a problem with my C code
or a problem with MS Visual C++.

Look at line 42.
Of what?
Precisely.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 26 '07 #28

P: n/a
On Jun 26, 12:19 am, Barry Schwarz <schwa...@doezl.netwrote:
>
This sure seems like a lot of work just avoid calling fgets.
MUCH more than worth it. I have replaced a black box that hangs with
much more transparent code that does not. If it did hang or crash, I
would have a much better idea of why.
>
Is 32 bytes really sufficient to hold the longest line in your file?
Excellent point. I will modify FReadLine() so that there is a maximum
to the number of characters that are read.
>
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"));

The cast is superfluous.
fMean=atof(strtok(NULL, "\t"));
warning C4244: '=' : conversion from 'double' to 'float', possible
loss of data
>
So far no problem.

So you have managed to camouflage the problem with no understanding of
what the real issue was?
I may have evaded the issue but have replaced a black box, that hangs
with no error message, with more transparent code. fscanf probably
hung because it was waiting for a particular input. It was not
suitable for this application since the expected input may not have
been in the input file. I think my modification has given me more
control and has made the code potentially more robust.

Thanks,
Peter.

Jun 30 '07 #29

P: n/a
PeterOut said:
On Jun 26, 12:19 am, Barry Schwarz <schwa...@doezl.netwrote:
>>
<snip>
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"));

The cast is superfluous.

fMean=atof(strtok(NULL, "\t"));
warning C4244: '=' : conversion from 'double' to 'float', possible
loss of data
The diagnostic message, whilst not compulsory, is useful, in that it
tells you that the conversion may not be able to retain all the
precision that would be available to you were you to use double.

Suppressing the diagnostic information is like taking the battery out of
your doorbell. There's still someone at the door, even if you can't
hear them any more.

Frankly, I'd use double. (And while I was at it, I'd use strtod instead
of atof and strtol instead of atoi.)

I certainly wouldn't pass strtok(NULL, "\t") to anything expecting a
string as an argument, in case the string you're parsing turned out to
be poorly formed.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 30 '07 #30

P: n/a
>On Jun 26, 12:19 am, Barry Schwarz <schwa...@doezl.netwrote:
>This sure seems like a lot of work just avoid calling fgets.
In article <11**********************@k79g2000hse.googlegroups .com>,
PeterOut <Ma**********@excite.comwrote:
>MUCH more than worth it. I have replaced a black box that hangs with
much more transparent code that does not. ...
I think Barry Schwarz's point was that you could just call fgets()
(*not* fscanf()!), instead of using another 15 or so lines of code
to re-implement fgets().
>I may have evaded the issue but have replaced a black box, that hangs
with no error message, with more transparent code. fscanf probably
hung because it was waiting for a particular input. It was not
suitable for this application since the expected input may not have
been in the input file.
Actually, fscanf() probably did *not* hang. Instead, since you
had code of the general form [%]:

some_sort_of_loop {
/* ignore result of */ fscanf(file, format, args);
if (feof(file)) break;
... do work ...
}

what would happen is that fscanf() would encounter "bad" input
and *leave the input in the stream*. The feof() test would say
"not EOF" -- fscanf() failed, yes, but not because of EOF -- and
on the next iteration, the fscanf() would again immediately
fail.

Using fgets() (instead of fscanf()) *is* a good idea. In general,
of the scanf family, the sscanf() function is the most useful /
least error-prone -- but even then there are pitfalls. Start with
fgets() (or equivalent), then break up the resulting lines as
desired, using sscanf() or the strto* functions, or whatever.

[% Any time anyone ignores the return value of an input-reading
function, whether that is one of the scanf family, or fgets(), or
getchar(), or whatever it may be, this is a nearly-certain sign
that the code is wrong. But at least the loop above avoids the
even more common beginner's error, which is to call feof() *before*
calling an input-reading function. Since feof() only tells you
*why* a previous read failed -- it has to be one that already failed
-- calling it *before* observing such a failure is just asking for
trouble. Calling it after observing failure is still somewhat
suspect, since many people seem not to realize that Standard C
provides for exactly two separate kinds of failure: "failure due
to ordinary end of file", for which feof() will say "yes" and
ferror() will say "no", and "failure due to bad floppy disk, or
cpu catching fire, or any other not-ordinary-EOF-disaster", for
which feof() will say "no" and ferror() will say "yes".]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 5 '07 #31

P: n/a
Chris Torek said:

<snip>
[...] many people seem not to realize that Standard C
provides for exactly two separate kinds of failure: "failure due
to ordinary end of file", for which feof() will say "yes" and
ferror() will say "no",
....sure.
and "failure due to bad floppy disk, or
cpu catching fire, or any other not-ordinary-EOF-disaster", for
which feof() will say "no" and ferror() will say "yes".]
Okay, okay, I hear you, but wait a minute... As the chicken says:

Ginger: Listen. We'll either die free chickens or we die trying.
Babs: Are those the only choices?

That is, does the Standard guarantee that (feof(fp) && ferror(fp))
evaluates to 0? Is there not scope for some condition in which, say, 32
bytes were requested, there were only 30 bytes left on the file, so EOF
would certainly be encountered, and only 16 bytes could be read because
the next bit of the file was in a bad sector, so that both Bad Things
are happening at once?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 5 '07 #32

P: n/a
In article <Xs*********************@bt.com>
Richard Heathfield <rj*@see.sig.invalidwrote:
>... That is, does the Standard guarantee that (feof(fp) && ferror(fp))
evaluates to 0?
Definitely not. In particular, since the error indicators are
"sticky" -- that is, they require use of clearerr(fp) to clear them
-- we could certainly have, for instance:

int c1, c2;
c1 = getc(stdin);
c2 = getc(stdin);

set both c1 and c2 to EOF, with the first EOF being due to the user
pressing the system's "EOF key" (^D or ^Z or whatever) and the
second being due to the user logging off ("terminating the session",
or whatever the system calls it). The first might set feof(fp),
and the second set ferror(fp).

I meant after a single error, but even then I am not sure:
>Is there not scope for some condition in which, say, 32
bytes were requested, there were only 30 bytes left on the file, so EOF
would certainly be encountered, and only 16 bytes could be read because
the next bit of the file was in a bad sector, so that both Bad Things
are happening at once?
That seems plausible.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 5 '07 #33

P: n/a
On Jul 5, 1:31 am, Chris Torek <nos...@torek.netwrote:
On Jun 26, 12:19 am, Barry Schwarz <schwa...@doezl.netwrote:
This sure seems like a lot of work just avoid calling fgets.

In article <1183227072.734131.307...@k79g2000hse.googlegroups .com>,

PeterOut <MajorSetb...@excite.comwrote:
MUCH more than worth it. I have replaced a black box that hangs with
much more transparent code that does not. ...

I think Barry Schwarz's point was that you could just call fgets()
(*not* fscanf()!), instead of using another 15 or so lines of code
to re-implement fgets().
Yes, I can see your point. I essentially re-implemented fgets() after
I added the maximum number of bytes to read. I will use fgets() since
it has probably been tested and optimized a lot more thoroughly than
my re-implemtation. I will also tryi using sscanf instead of
strtok(). However, it is beginning to appear that my problem is a
deadlock which tends to arise in multihtreaded programming.

Thanks,
Peter.
Jul 11 '07 #34

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Suppressing the diagnostic information is like taking the battery out of
your doorbell.
Doorbells are battery-powered in England? Yet another way that
it's such a foreign country...
--
Ben Pfaff
http://benpfaff.org
Jul 11 '07 #35

P: n/a
Ben Pfaff wrote, On 11/07/07 03:28:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Suppressing the diagnostic information is like taking the battery out of
your doorbell.

Doorbells are battery-powered in England?
Some of them are, some are not.
Yet another way that
it's such a foreign country...
No, yours is the foreign country, what's worse it is full of foreigners ;-)
--
Flash Gordon
Jul 11 '07 #36

P: n/a
Ben Pfaff said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Suppressing the diagnostic information is like taking the battery out
of your doorbell.

Doorbells are battery-powered in England?
Well, some are, yes. And some aren't. And some, like mine, or more sort
of kinda. My setup is as follows:

On the door, a box for the pushbutton. It contains a battery and a radio
transmitter.

In the kitchen, a box plugged into the mains. It contains a radio
receiver and the actual speaker.

It's a completely wireless system. Even the sound box has no power cable
- the three pins are right there on the box, so you just stick it in
the wall.

The only minor disadvantage is that, when you first install it, you have
to fiddle around with the channel settings a bit to avoid clashing with
other similar radio devices in the area. It can take a few days to work
out the best channel. Apart from that, though, it's great.

Yet another way that it's such a foreign country...
England isn't foreign. You must be thinking of somewhere else.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 11 '07 #37

P: n/a
Ben Pfaff wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Suppressing the diagnostic information is like taking the battery
out of your doorbell.

Doorbells are battery-powered in England? Yet another way that
it's such a foreign country...
Some are in the USA also. A battery is considerably cheaper than a
transformer, and works even when the power is out! This is
magnified when the mains are 250 VAC. Of course, with the usual
lack of maintenance, the doorbells don't work.

--
<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

Jul 11 '07 #38

This discussion thread is closed

Replies have been disabled for this discussion.