LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE
User Name
Password
SUSE / openSUSE This Forum is for the discussion of Suse Linux.

Notices


Reply
  Search this Thread
Old 10-02-2007, 12:48 AM   #1
sriram_16a
LQ Newbie
 
Registered: Aug 2006
Location: Chennai, India
Distribution: SuSE,Slackware,Knoppix
Posts: 24

Rep: Reputation: 15
Exclamation fstream.h is creating erratic binary programs


Hey guys, I am using g++ to compile my c++ projects and i noticed that the code is not giving the correct output. The last data written in the file is output twice. My c++ version is 4.1.2 I used the command
g++ -o test test.cpp
I have tried creating textfiles with fstream; they worked perfect.
Here is the code:



//test.cpp
#include<iostream>
#include<fstream>
using namespace std;

class a1{int i,j;
public:
void getdat()
{
cin>>i>>j; //input 2 test variables
}
void putdata()
{
cout<<i<<j;
}
}a;
int main(int argc, char *argv[])
{
fstream f("abc.dat",ios::binary|ios:ut|ios::app);
int i;
for(i=0;i<2;i++) //input 2 test objects
{a.getdat();
f.write((char*)&a,sizeof(a));
}
f.close();
f.open("abc.dat",ios::binary|ios::in);
int w=0;
while(f)
{
f.read((char*)&a,sizeof(a));
w++;
//a.putdata();
}
f.seekg(0);
for(i=0;i<w;i++)
{
f.read((char*)&a,sizeof(a));
a.putdata();
}
f.close();
return 0;
}
 
  


Reply

Tags
g++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl programs downloading as binary via cgi/apache? jon_k Linux - Software 1 02-22-2007 05:28 AM
Creating/Distributing Linux Binary Files Sava Programming 3 08-29-2006 09:31 AM
Creating binary RPMS statically! Sunilsbjoshi Linux - Software 2 10-12-2005 12:10 AM
Creating shortcuts to programs piggysmile Linux - Software 3 05-27-2004 08:43 AM
creating a flat binary file with gcc wsimmons Programming 2 01-08-2002 11:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE

All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration