473,569 Members | 2,580 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 #1
37 4932
On Jun 18, 10:19 am, PeterOut <MajorSetb...@e xcite.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(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.
Dear Peter,

Your are not closing the file.
Use fclose for closing the file.

Thanks and regards,
Amal P.

Jun 18 '07 #2
On Jun 17, 9:32 pm, Amal P <enjoyam...@gma il.comwrote:
On Jun 18, 10:19 am, PeterOut <MajorSetb...@e xcite.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(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.

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

"PeterOut" <Ma**********@e xcite.comwrote in message
news:11******** **************@ c77g2000hse.goo glegroups.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(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);
%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
On Sun, 17 Jun 2007 18:19:37 -0700, PeterOut <Ma**********@e xcite.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(cha r *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
ErrorOpeningFi le(csFileName);

do
{
// It randomly hangs on the followinf
line
How do you determine that?
> iRead=fscanf(fp InFile, "%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
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(fp InFile, "%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
On Jun 18, 3:19 am, PeterOut <MajorSetb...@e xcite.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(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);
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::istringstr eam 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 18 '07 #7
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.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 19 '07 #8
On Jun 17, 10:06 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"PeterOut" <MajorSetb...@e xcite.comwrote in message

news:11******** **************@ c77g2000hse.goo glegroups.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(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);

%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
On Jun 18, 12:58 am, Martin Ambuhl <mamb...@earthl ink.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(fp InFile, "%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

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

Similar topics

4
3028
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...
7
5405
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
5552
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
7628
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...
0
8145
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...
1
7694
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7993
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...
0
6317
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...
0
5245
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2128
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
0
970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.