#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?

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

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 <iostream>. That won't work I guess.

You're #including <string>, but then using a <char*> instead of a <string>!

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 <string> instead of a <char*>, so you won't run into memory troubles.

EDIT:

cin >> <char*> should work, but be sure to first make the <char*> 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.

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 <iostream>. That won't work I guess.

You're #including <string>, but then using a <char*> instead of a <string>!

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 <string> instead of a <char*>, 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;
}

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

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 <char*> when you've included <string> for example.

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

Trying to post again; using code tags

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

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

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

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

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

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.