#include<fstream.h>
#include<iostream.h>
#include<string.h>
using namespace std;
class emp
{
public:
char *per;
int i;
int write2()
{
ofstream out("ss.txt",ios::app | ios::binary );
cout<<"Enter some character ....\n";
per = new char[100];
cin.getline(per,100,'\n')
out.write("\n",1);
out.write(per,strlen(per));
out<< "\n";
per = "asdfgh";
out.write(per,strlen(per));
out.write((char *)&i,5);
out.write(per,strlen(per));
return 0;
}
int read_2()
{
char * buffer;
long size;
ifstream in("ss.txt",ios::in|ios::binary|ios::ate);//try without any of these flags.
size = in.tellg();
in.seekg (0, ios::beg);
buffer = new char [size];
in.read (buffer, size);
in.close();
cout << "the complete file is in a buffer...\n";
cout<<buffer;
delete[] buffer;
return 0;
}
};
void main()
{
emp e;
e.write2();//Writes to a file in binary format even if the input data has space. eg:-
e.read_2();//Reads a binary file.
}
mksakeesh
0
Newbie Poster
Recommended Answers
Jump to PostWell.. whats your problem ?
Jump to Postline 26: that just caused a memory leak. Where did the memory allocated by new operator on line 16 go??? Answer: the Bit Bucket. You have to call delete[] before reusing that pointer on line 26.
>>I am not able to perform the step "out.write((char *)&i,5);",
Suggestion: Create a …
Jump to Posttry changing the line like this:
out.write(reinterpret_cast<char*>( &i ) , sizeof i);
That should save the integer, but this saves it in the form of binary, so its not readable if you open it in notepad.
All 11 Replies
William Hemsworth
1,339
Posting Virtuoso
mksakeesh
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
William Hemsworth
1,339
Posting Virtuoso
mksakeesh
0
Newbie Poster
William Hemsworth
1,339
Posting Virtuoso
mksakeesh
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
mksakeesh
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.