Problem in using fscanf

R

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==NULL){
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(fdataptr,"%x", &ctrl)==EOF)
break;
} while (x);

}


data.txt is having the values

11111111
22222222
33333333
44444444
55555555
66666666
77777777
88888888
99999999




Thanks & Regards,
RSK...
 
M

Mark Bluemel

rsk said:
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]
 
M

Mark Bluemel

rsk said:
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.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Benoit said:
if (fscanf(fdataptr,"%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.
 
R

Richard Heathfield

Benoit Lefebvre said:
if (fscanf(fdataptr,"%x", &ctrl)==EOF)

should be

if (ctrl==EOF)

No, it shouldn't.

The proper solution is to structure the loop more intelligently:

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

John Gordon

In said:
do {
fscanf(fdataptr,"%x", &ctrl);
printf ("\n\n data is 0x%x \n\n",ctrl);
if (fscanf(fdataptr,"%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.
 
P

Peter 'Shaggy' Haywood

Groovy hepcat rsk was jivin' in comp.lang.c on Mon, 30 Jul 2007 11:20
pm. It's a cool scene! Dig it.
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;

The code you posted will not even compile. You need to include some
headers. You need stdio.h for printf(), fscanf() and fopen() as well as
stdlib.h for exit() (and EXIT_SUCCESS and EXIT_FAILURE - see below).

#include <stdio.h>
#include said:

int main(void)

is better.
{
FILE *fdataptr;
int x;
int ctrl;
x=1;

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

Error messages typically go to the standard error stream (stderr).
Also, since stderr is typically line buffered (and stdout is usually
fully buffered or line buffered), you should end the output with a
newline character. There is no need to begin the output with a newline.

frintf(stderr, "Cannot open File data.txt\n");

Returning would be better (IMHO) than calling exit() from main(). In
either case, portable exit values are 0, EXIT_SUCCESS and EXIT_FAILURE.

return EXIT_FAILURE;
}

do {
fscanf(fdataptr,"%x", &ctrl);
printf ("\n\n data is 0x%x \n\n",ctrl);

Sure you've got enough newlines there? Your output is going to take up
alot of vertical space, with many large gaps. It's just a suggestion,
but wouldn't it be better to output fewer blank lines? Feel free to
ignore this if you wish.

printf("data is 0x%x\n", ctrl);
if (fscanf(fdataptr,"%x", &ctrl)==EOF)
break;

Other people have already told you you're reading two numbers in this
loop. However, what they failed to tell you is that this is a very poor
way to control a loop.
} while (x);

The value of x is never changed. The only way to end this loop,
therefore, is to break out of it. Very poor! A much better loop would
be the following:

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

This uses the return value of fscanf() to control the loop. This is as
it should be, since you want to enter the loop body as long as you can
read a number. You can also do away with the unused variable x.
Now, since this is the end of a function returning int, it's a good
idea to return an int here. (See above for portable exit values.)

return 0;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top