954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need to write an integer to a binary file

<pre><code>#include&lt;fstream.h&gt;
#include&lt;iostream.h&gt;
#include&lt;string.h&gt;
using namespace std;
class emp
{
	public:
		char *per;
int i;
	int write2()
		{		
			ofstream out(&quot;ss.txt&quot;,ios::app | ios::binary );

			cout&lt;&lt;&quot;Enter some character ....\n&quot;;

			per = new char[100];

			cin.getline(per,100,'\n')
			
			out.write(&quot;\n&quot;,1);
			
			out.write(per,strlen(per));
			
			out&lt;&lt; &quot;\n&quot;;

			per = &quot;asdfgh&quot;;

			out.write(per,strlen(per));

			out.write((char *)&amp;i,5);
			
			out.write(per,strlen(per));
			
			return 0;
		}
		int read_2()
		{
			char * buffer;

			long size;

			ifstream in(&quot;ss.txt&quot;,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 &lt;&lt; &quot;the complete file is in a buffer...\n&quot;;

			cout&lt;&lt;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.
}</code></pre>
mksakeesh
Newbie Poster
17 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Well.. whats your problem ?

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

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.

mksakeesh
Newbie Poster
17 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

Can you please explain what a bit bucket is?

mksakeesh
Newbie Poster
17 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

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( &i ) , sizeof i);", has run well, but when file is opened it is showing some unwanted characters. I have opened the file in editplus.

mksakeesh
Newbie Poster
17 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

>>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));

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
Can you please explain what a bit bucket is?

Click here

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

mksakeesh
Newbie Poster
17 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 
This is writing some unwanted character to the binary file. I have opened the file in Edit Plus.


Of course it does -- afterall that's what abinary file looks like. google for definition of binary file See post #5 above.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You