473,695 Members | 2,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(csFileNam e, "r")) == NULL) return
ErrorOpeningFil e(csFileName);

do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fp InFile, "%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
37 4958
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
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.securityfoc us.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
On Sun, 24 Jun 2007 07:04:37 -0700, PeterOut <Ma**********@e xcite.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
On Jun 23, 2:50 pm, Johan Bengtsson <qwerty...@hotm ail.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(csFileNam e, "r")) == NULL) return
ErrorOpeningFil e(csFileName);
do
{
// It randomly hangs on the followinf
line
iRead=fscanf(fp InFile, "%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(AfxCheck Memory());

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_F ILE;
i = (csBuffer[0] == '\n')? 0 : 1;
while ((csBuffer[i] = fgetc( fpInputFile )) != '\n')
{
if (csBuffer[i++] == EOF) return ERROR_READING_F ILE;
}
csBuffer[i] = '\0';

return ERROR_NONE;
}

if ((fpFile = fopen(csFileNam e, "r")) == NULL)
{
// Error handling
}
{
float fMean;
long lX, lY, lLastX = fppPlane->iColumns-1, lLastY = fppPlane-
>iRows-1;
char csBuffer[32];

do
{
if ((enErrorNumber =FReadLine(fpFi le, csBuffer))!=ERR OR_NONE) break;
lX=atoi(strtok( csBuffer, "\t"));
lY=atoi(strtok( NULL, "\t"));
fMean=(float)at of(strtok(NULL, "\t"));
} while (lX < lLastX || lY < lLastY);
}
fclose(fpFile);

So far no problem.

Thanks again for your help,
Peter.

Jun 26 '07 #24
On Jun 24, 11:10 am, CBFalconer <cbfalco...@yah oo.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
On Mon, 25 Jun 2007 19:15:44 -0700, PeterOut <Ma**********@e xcite.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_F ILE;
}
csBuffer[i] = '\0';

return ERROR_NONE;
}
This sure seems like a lot of work just avoid calling fgets.
>
if ((fpFile = fopen(csFileNam e, "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(fpFi le, csBuffer))!=ERR OR_NONE) break;
lX=atoi(strtok( csBuffer, "\t"));
lY=atoi(strtok( NULL, "\t"));
fMean=(float)at of(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
PeterOut said:
On Jun 24, 11:10 am, CBFalconer <cbfalco...@yah oo.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
On Mon, 25 Jun 2007 19:18:21 -0700, in comp.lang.c , PeterOut
<Ma**********@e xcite.comwrote:
>On Jun 24, 11:10 am, CBFalconer <cbfalco...@yah oo.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
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(fpFi le, csBuffer))!=ERR OR_NONE) break;
lX=atoi(strtok( csBuffer, "\t"));
lY=atoi(strtok( NULL, "\t"));
fMean=(float)at of(strtok(NULL, "\t"));

The cast is superfluous.
fMean=atof(strt ok(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
PeterOut said:
On Jun 26, 12:19 am, Barry Schwarz <schwa...@doezl .netwrote:
>>
<snip>
do
{
if ((enErrorNumber =FReadLine(fpFi le,
csBuffer))!=ERR OR_NONE) break; lX=atoi(strtok( csBuffer,
"\t")); lY=atoi(strtok( NULL, "\t"));
fMean=(float)at of(strtok(NULL, "\t"));

The cast is superfluous.

fMean=atof(strt ok(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3052
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an element of an array (eventually will be other things, but I'm sticking to int's for now). I.e.: 0 1 1 1 1 1 2 1 1 etc... The problem is it'll read and print all but the last line. Is there
7
5443
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
13
510
by: PeterOut | last post by:
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;
59
5573
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading the + character, and then sending the remainder into fscanf() like
0
8572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8984
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8825
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6491
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4340
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.