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

Writing To a File in Binary Mode

#include<fstream.h>
#include<string.h>
using namespace std;
class emp
{
	public:
		char *per;
		void write()
		{		
			ofstream out("ss.txt",ios::app | ios::binary);
			cout<<"Enter some character ....\n";

			cin>>*per;
			//out.write(per,sizeof(per));
			out.write(per,strlen(per));
			
		}
		
};

void main()
{
	emp e;
	e.write();
	
}

Why this is not working?

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

Oh boy, where to start..
First, learn to use code tags, its not difficult.
Give more details on your problem, and show the errors you are getting.
Dont use void main.
Try posting this again, but this time.. properly
and then I will help you with your problem.

Read this thread which you should have read anyway before posting.
Read This Before Posting

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

What errors are you getting?
Why isn't your code in [code] tags?
... or indented for that matter?

You should post this along with your compiler.

You're trying to use cout without #including . That won't work I guess.

You're #including , but then using a instead of a !

On top of that, you're declaring a pointer that's pointing to ANY place in the memory and ordering cin to write the input to that? I think that's what you were trying to do.

I don't know how cin and char* work together, but it's better to make per a instead of a , so you won't run into memory troubles.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

EDIT:

cin >> should work, but be sure to first make the point to some allocated memory with the new operator. And keep in mind that it won't parse spaces.

To parse spaces as well, use cin.getline();

Your output mechanism should work, but you might want to get rid of ios::app, just try it with ios::binary alone.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

What errors are you getting? Why isn't your code in [code] tags? ... or indented for that matter?

You should post this along with your compiler.

You're trying to use cout without #including . That won't work I guess.

You're #including , but then using a instead of a !

On top of that, you're declaring a pointer that's pointing to ANY place in the memory and ordering cin to write the input to that? I think that's what you were trying to do.

I don't know how cin and char* work together, but it's better to make per a instead of a , so you won't run into memory troubles.


I am just trying to learn thing My self. Sorry for the improper coding standard.

I really don't understand, what you both mean by saying 'code tags'

I am using Borland C++ compiler.

I am trying to enter inputs from keyboard to a file in binary format. I am not getting any error instead, if i enter in keyboard as "sfdahjkllllllllg", it will be written in the ss.txt file as 's“A', now if i enter in keyboard as "asdfgeturit", then in file it will be written as 'a“A'

Let me repost the code again with modifications:-

#include<fstream.h>
#include<iostream.h>
#include<string.h>
using namespace std;
class emp
{
	public:
		char *per;
		void write()
		{		
			ofstream out("ss.txt",ios::app | ios::binary);
			cout<<"Enter some character ....\n";

			cin>>*per;
			//out.write(per,sizeof(per));
			out.write(per,strlen(per));
		}
};

int main()
{
	emp e;
	e.write();
	return 0;
}
mksakeesh
Newbie Poster
17 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

>I really don't understand, what you both mean by saying 'code tags'
What ?!, are you serious. There are so many places on this site that fully explains how they work.. Try reading the link I posted.

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

First: read the link william posted. It's important.

And...
I've said some more, also answer those questions. It doesn't make sense you're using a when you've included for example.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

>>cin>>*per;
As mention before, per is a pointer that just points to some random memory location. and *per only references the first byte, not the entire string. Unless the purpose of this assignment is to learn character arrays and pointers, then you would be better off using std::string.

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

Trying to post again; using code tags

<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;
		void write()
		{		
			ofstream out(&quot;ss.txt&quot;,ios::app | ios::binary);
			cout&lt;&lt;&quot;Enter some character ....\n&quot;;

			cin&gt;&gt;*per;
			//out.write(per,sizeof(per));
			out.write(per,strlen(per));
		}
};

int main()
{
	emp e;
	e.write();
	return 0;
}</code></pre>


I have used char *per because 'write' function only accepts pointer to character.

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

>>I have used char *per because 'write' function only accepts pointer to character.
False. You can put anything you want to there -- just typecast it to char*


But you still failed to correct the problem. per is still an unallocated pointer which will crash your program.

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

>>I have used char *per because 'write' function only accepts pointer to character. False. You can put anything you want to there -- just typecast it to char*

But you still failed to correct the problem. per is still an unallocated pointer which will crash your program.


Thanks ...... this is working now; i changed the program like this

#include<fstream.h>
#include<iostream.h>
#include<string.h>
using namespace std;
class emp
{
	public:
		char *per;
		void write()
		{		
			ofstream out("ss.txt",ios::app | ios::binary);
			cout<<"Enter some character ....\n";
			per = new char[100];
			cin>>per;
			//out.write(per,sizeof(per));
			out.write(per,strlen(per));

		}
		
};

void main()
{
	emp e;
	e.write();
	
}
mksakeesh
Newbie Poster
17 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

EDIT: Didn't see the new[] Ouch. My bad, 100%.

Job well done on the program. However, when write is now called multiple times, each time it will allocate 100 bytes of memory to the pointer. This is called a memory leak. To fix that, make sure you only allocate the memory once (call new once).

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You