473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem in reading datafile

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[b]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source1 [%d] stream data is %x\n",b,source1[b]);

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...

--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

Sep 6 '07 #1
3 1431
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.
Sep 6 '07 #2
In <f0******************************@localhost.talkab outprogramming.com"rsk" <kr******@yahoo.co.inwrites:
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().

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

Sep 6 '07 #3
rsk wrote:
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[b]);
data_count++;
printf("\n Data count is %d", data_count);
printf("\n Source1 [%d] stream data is %x\n",b,source1[b]);

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.

--
Chris "win-win" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Sep 6 '07 #4

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

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
4
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I declare lets say a 1000 of those classes and put them...
6
by: me | last post by:
Hi guys - the question is in the subject line. I thought of one quick way: std::ifstream input("myfile.dat") ; std::istreambuf_iterator beg(input), end ; std::vector DataFile(beg,end) ;
6
by: vasilijepetkovic | last post by:
Hello All, I have a problem with the program that should generate x number of txt files (x is the number of records in the file datafile.txt). Once I execute the program (see below) only one...
1
by: tinks | last post by:
I am getting a linking error when I do something like this: ifstream dataFile; dataFile.open(dataFileName_, ios::in); while(dataFile) { dataFile.getline(buffer, MAX_DATA_FILE_LINE_LEN); //...
2
by: John Jumper | last post by:
I am using an XmlDataSource in VS2005Beta1 and setting the TransformFile property in my ASCX and it is finding both files. However, the resulting data does not contain any of the sub nodes of the...
9
by: Alex Buell | last post by:
I have a small text file which consist of the following data: ]] And the code I've written is as follows: ]] The trouble is, I can't work out why it goes into an infinite loop reading the...
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import...
3
by: jewel87 | last post by:
Hello everyone, I'm having a problem with reading data from file. In my form i have a function which should read the data from a file into a vector myProductList. void __fastcall...
7
by: tonyaim83 | last post by:
Hi... My input file is as follows :- A<-B<-C<-D A<-C<-E<-F<-P ....... ..... What i want is to first store the given input as follows :- {{{A,B},{B,C},{C,D}},{{A,C},{C,E},{E,F},{F,P}}} and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.