Problem in reading datafile

R

rsk

Hi Friends,

I have the following code which reads the hexadecimal data from the
"data.txt" file into the arrays.

But when i run this code it is reading some garbage data after reading all
the data.

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

The data.txt file contains the data as:
11111111
22222222
33333333
44444444
55555555
66666666
77777777
88888888
99999999
aaaaaaaa
--------------------------------------------------

The code is as follows:

#include <stdio.h>
#include <math.h>

main ()
{
FILE *fdataptr ;
int data_count ;
int data [30] ;
int source0 [31] ;
int source1 [31] ;
int source2 [31] ;
int source3 [31] ;
int source4 [31] ;
int a ;
int b ;
int c ;
int d ;
int e ;


data_count =0;
a=0;
b=0;
c=0;
d=0;
e=0;


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

while(!feof(fdataptr)) {
fscanf(fdataptr,"%x", &source0[a]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source0 [%d] stream data is %x\n",a,source0[a]);

fscanf(fdataptr,"%x", &source1);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source1 [%d] stream data is %x\n",b,source1);

fscanf(fdataptr,"%x", &source2[c]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source2 [%d] stream data is %x\n",c,source2[c]);

fscanf(fdataptr,"%x", &source3[d]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source3 [%d] stream data is %x\n",d,source3[d]);

fscanf(fdataptr,"%x", &source4[e]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source4 [%d] stream data is %x\n",e,source4[e]);

a++;
b++;
c++;
d++;
e++;

}//close of while
}//close of else
}//close of main

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

I got the output as

data.txt File is Opened Sucessfully.

Data count is 1
Source0 [0] stream data is 11111111

Data count is 2
Source1 [0] stream data is 22222222

Data count is 3
Source2 [0] stream data is 33333333

Data count is 4
Source3 [0] stream data is 44444444

Data count is 5
Source4 [0] stream data is 55555555

Data count is 6
Source0 [1] stream data is 66666666

Data count is 7
Source1 [1] stream data is 77777777

Data count is 8
Source2 [1] stream data is 88888888

Data count is 9
Source3 [1] stream data is 99999999

Data count is 10
Source4 [1] stream data is aaaaaaaa

Data count is 11
Source0 [2] stream data is 0

Data count is 12
Source1 [2] stream data is 0

Data count is 13
Source2 [2] stream data is 0

Data count is 14
Source3 [2] stream data is 40002233

Data count is 15
Source4 [2] stream data is 0


Can you please tell me why iam getting the values
0,0,0,40002233 and 0 which are not available in the data.txt file.


Thanks in advance.
Rsk...
 
M

Mark Bluemel

rsk wrote:
....
while(!feof(fdataptr)) {
fscanf(fdataptr,"%x", &source0[a]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source0 [%d] stream data is %x\n",a,source0[a]);


I suggest you read the FAQ (http://c-faq.com), especially the section on
IO. This should help you understand why your loop control is mistaken.
 
J

John Gordon

In said:
But when i run this code it is reading some garbage data after reading all
the data.
while(!feof(fdataptr)) {
fscanf(fdataptr,"%x", &source0[a]);

This is why.

feof() is not true until you've attempted to read *past* end-of-file.

If there are ten lines in the file and you've done ten fscanf()s you
have not reached end-of-file yet, and thus feof() will not return true.

End-of-file will only trigger once you attempt the eleventh fscanf().
 
C

Chris Dollin

rsk said:
I have the following code which reads the hexadecimal data from the
"data.txt" file into the arrays.

I'm not going to answer your question (because I see others have done
that); I'm going to make a comment about your code.

Ugh.


When I see:
int source0 [31] ;
int source1 [31] ;
int source2 [31] ;
int source3 [31] ;
int source4 [31] ;
int a ;
int b ;
int c ;
int d ;
int e ;

(and ignoring that you don't take the opportunity to initialise your
variables /right there/), and:
fscanf(fdataptr,"%x", &source0[a]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source0 [%d] stream data is %x\n",a,source0[a]);

fscanf(fdataptr,"%x", &source1);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source1 [%d] stream data is %x\n",b,source1);

fscanf(fdataptr,"%x", &source2[c]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source2 [%d] stream data is %x\n",c,source2[c]);

fscanf(fdataptr,"%x", &source3[d]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source3 [%d] stream data is %x\n",d,source3[d]);

fscanf(fdataptr,"%x", &source4[e]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source4 [%d] stream data is %x\n",e,source4[e]);

a++;
b++;
c++;
d++;
e++;


I shudder. Why, why, why is essentially /the same code/ being repeated
over and over and over again again? That blocklet could quite tidily
be turned into a function, and your code would be shorter and clearer
for it.

And you'd probably end up reducing the number of variables by a factor
of five, too.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top