473,757 Members | 8,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem in using fscanf

rsk
Hi Friends,

I have written a code which suppose to read all the numbers from a hex
file,But to my surprise the code is skiping every alternate value.Don't
know why?

Can you please help me in solving this problem.

The code is as follows;
main ()
{
FILE *fdataptr;
int x;
int ctrl;
x=1;

// Open the file containing data
fdataptr = fopen("data.txt ", "r");
if(fdataptr==NU LL){
printf("\n Cannot open File data.txt");
exit(1);
}

do {
fscanf(fdataptr ,"%x", &ctrl);
printf ("\n\n data is 0x%x \n\n",ctrl);
if (fscanf(fdatapt r,"%x", &ctrl)==EOF)
break;
} while (x);

}
data.txt is having the values

11111111
22222222
33333333
44444444
55555555
66666666
77777777
88888888
99999999


Thanks & Regards,
RSK...

Jul 30 '07 #1
10 3668
rsk wrote:
Hi Friends,

I have written a code which suppose to read all the numbers from a hex
file,But to my surprise the code is skiping every alternate value.Don't
know why?
Because you read and ignore every second number?
[snip]
do {
fscanf(fdataptr ,"%x", &ctrl);
printf ("\n\n data is 0x%x \n\n",ctrl);
if (fscanf(fdatapt r,"%x", &ctrl)==EOF)
break;
} while (x);
Jul 30 '07 #2
rsk
Can you correct me to print all the data values

Jul 30 '07 #3
rsk wrote:
Can you correct me to print all the data values
Please quote some context when you are replying.

I'm not going to correct your program for you. I've already told you
what you are doing wrong, and I think it's now down to you to correct it.
Jul 30 '07 #4
rsk
Can you correct me to print all the data values

Jul 30 '07 #5
"rsk" <kr******@yahoo .co.inwrites:
Can you correct me to print all the data values
Look how many times you are calling fscanf. Hint : too many times in the
loop.

Aha!
Jul 30 '07 #6
On Jul 30, 10:13 am, "rsk" <krs_1...@yahoo .co.inwrote:
Can you correct me to print all the data values
if (fscanf(fdatapt r,"%x", &ctrl)==EOF)

should be

if (ctrl==EOF)

Jul 30 '07 #7
Benoit Lefebvre wrote:
On Jul 30, 10:13 am, "rsk" <krs_1...@yahoo .co.inwrote:
>Can you correct me to print all the data values

if (fscanf(fdatapt r,"%x", &ctrl)==EOF)

should be

if (ctrl==EOF)
No, it shouldn't be that. If fscanf doesn't read ctrl, it doesn't set ctrl;
ctrl isn't somehow set to EOF.
Jul 30 '07 #8
Benoit Lefebvre said:
On Jul 30, 10:13 am, "rsk" <krs_1...@yahoo .co.inwrote:
>Can you correct me to print all the data values

if (fscanf(fdatapt r,"%x", &ctrl)==EOF)

should be

if (ctrl==EOF)
No, it shouldn't.

The proper solution is to structure the loop more intelligently:

while(fscanf(fd ataptr,"%x", &ctrl) == 1)
{
printf ("\n\n data is 0x%x \n\n",ctrl);
}

--
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 30 '07 #9
In <29************ *************** ***@localhost.t alkaboutprogram ming.com"rsk" <kr******@yahoo .co.inwrites:
do {
fscanf(fdataptr ,"%x", &ctrl);
printf ("\n\n data is 0x%x \n\n",ctrl);
if (fscanf(fdatapt r,"%x", &ctrl)==EOF)
break;
} while (x);
This loop calls fscanf() twice. You're only printing the result from the
first call. The results from the second fscanf() are discarded.

Change your loop to only call fscanf() once.

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.co m B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Jul 30 '07 #10

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

Similar topics

10
4190
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like this: 456 5 1.txt%&'
2
4091
by: ArmedCoder | last post by:
Hi, im just learning about multithreading for a program i am writin that needs to read from multiple camreas attached to the computer a the same time. I know how to create threads and pass around info, and i know a bi about mutexes but this is not where the problem resides. Every time the thread goes to open the device at /dev/video* the threa just stops running as far as i can see. i put printf's all over the place and it prints right...
7
2828
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I enter a number, can I use to call a particular node ? eg enter a number: 3 calling node of number 3 is it possible ?
5
424
by: learner | last post by:
I have datafiles like this: 0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1 0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2 0 1941 0.00 0.00 0.00 0.00 0.52 0.00 0.00 0.17 1.07 0.09 3 0 1941 0.04 0.00 0.00 0.00 0.00 0.62 0.00 0.01 0.00 0.00 4 0 1941 0.00 0.02 0.00 0.00 0.00 0.22 0.00 0.00 0.00 0.16 5 0 1941 0.00 0.00 0.00 0.00 0.09 0.04 0.00 0.00 0.00 0.00 6 0 1941 0.00...
23
2411
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file, there are lots of errors and warnings in compiling stage. I am really confused about this problem because I'm not even familiar with C++ and my code only includes simple C functions. Can anybody please tell me what's my mistake? Is there any other...
14
2935
by: abhishekkarnik | last post by:
Hi, I am trying to read an exe file and print it out character by character in hexadecimal format. The file goes something like this in hexadecimal 0x4d 0x5a 0x90 0x00 0x03 .... so on When I read it into my array " two_bytes" which has 16 characters in total, I get each of these values read correctly, but when I attempt printing them out, it prints something like this: 0x4d 0x5a 0xffffff90 0x00 0x03 .... so on. I have no clue why. How...
18
2098
by: Scott | last post by:
Hi, a problem with this following code is really bugging me. tform = fopen(country, "r"); fseek(tform, 9L, SEEK_SET); fgets(player2, 38, tform); printf("Player Name (save): %s", player); printf("Player Name (form): %s", player2);
4
1434
by: neha_chhatre | last post by:
i have a wierd problem...iam not able to understand wat is goin wrong...i have written a code which reads the values form a text file (it contains lot of values like time=1.147279,value=240.66 and so on................) the code is workin well for the firs five iterations...but thn it isnt doing the computations properly after the 5th iteration (sumthing is goin wrong in the if loop) pls help me out with this Iam posting the entire code...
24
6011
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h> #include<string.h> void main(){ FILE *fp,*ftemp;
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
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
8737
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
7286
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
6562
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
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...
1
3829
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
3
3399
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.