473,802 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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==NU LL){
printf("\n Cannot open data.txt file.\n");
exit(1);
}
else {
printf("\n data.txt File is Opened Sucessfully.\n" );

while(!feof(fda taptr)) {
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 1462
rsk wrote:
....
while(!feof(fda taptr)) {
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.t alkaboutprogram ming.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(fda taptr)) {
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.co m 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
9860
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
4
3163
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 in a vector. Can I write that vector directly to a binary file?
6
4137
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
1813
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 file (instead of x files) is created. The file created is based on the last record in datafile.txt. The program is as follows:
1
2887
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); // This line creates linking issue on solaris }
2
1977
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 generated document. I have tested my XSL by viewing the generated XML and it properly transforms the DataFile in the UI editor for the XSL. I have verified the XmlDataSource is finding the DataFile by removing the TransformFile property which...
9
2764
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 information from the text file! Can anyone enlighten me as to what I am doing wrong?
2
2965
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 namespace="System.Web.UI.WebControls" %> <%@ import namespace="System.Data" %> <%@ import namespace="System.Data.OleDb" %>
3
1232
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 TForm1::FormCreate(TObject *Sender) { myProductList.ReadData(i,size); myProductList.getProduct(i)->display(ProductIDLabel,
7
1543
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 then make a check if the pair is present in particular subset of a set.
0
10538
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
10285
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
10063
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
9115
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...
0
5494
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...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.