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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I am not able to perform the step "out.write((char *)&i,5);", I am trying to log the count for each time we call the write2() function, but i am not able to enter integer value to a binary file.

line 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 static counter at the top of the write2() function, increment it, then write it. That way it will not have to depend on something else writing it

int write2()
{
    static int counter = 0;
    counter++;
    out.write(&counter, sizeof(int));
    // rest of function here
}

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

Any memory that you allocate should be deleted using the delete keyword otherwise it will case a memory leak. Even though AD has already said it, on line 26 you have assigned the pointer per to point at the characters "asdfgh" and you have ditched the 100 characters that you allocated on line 16. If you want to fill that array with a string, use strcpy.

Suggestion of AD has produced compilation error as:-
Error E2034 binaryfilewriting.cpp 64: Cannot convert 'int *' to 'const char *' i
n function emp::write2()
Error E2342 binaryfilewriting.cpp 64: Type mismatch in parameter 's' (wanted 'co
nst char *', got 'int *') in function emp::write2()
*** 2 errors in Compile ***

line 64 here is "out.write(&i, sizeof(i));"

and trying with "out.write(reinterpret_cast<char*>( &i ) , sizeof i);", has run well, but when file is opened it is showing some unwanted characters. I have opened the file in editplus.

>>Error E2034 binaryfilewriting.cpp 64: Cannot convert 'int *' to 'const char *' i

>> out.write(&counter, sizeof(int));
That should have been typecast, like this: out.write((char*)&counter, sizeof(int));

>>Error E2034 binaryfilewriting.cpp 64: Cannot convert 'int *' to 'const char *' i

>> out.write(&counter, sizeof(int));
That should have been typecast, like this: out.write((char*)&counter, sizeof(int));

This is writing some unwanted character to the binary file. I have opened the file in Edit Plus.

This article has been dead for over six months. Start a new discussion instead.