Writing To a File in Binary Mode

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 15
Reputation: mksakeesh is an unknown quantity at this point 
Solved Threads: 0
mksakeesh mksakeesh is offline Offline
Newbie Poster

Writing To a File in Binary Mode

 
0
  #1
Aug 11th, 2008
  1. #include<fstream.h>
  2. #include<string.h>
  3. using namespace std;
  4. class emp
  5. {
  6. public:
  7. char *per;
  8. void write()
  9. {
  10. ofstream out("ss.txt",ios::app | ios::binary);
  11. cout<<"Enter some character ....\n";
  12.  
  13. cin>>*per;
  14. //out.write(per,sizeof(per));
  15. out.write(per,strlen(per));
  16.  
  17. }
  18.  
  19. };
  20.  
  21. void main()
  22. {
  23. emp e;
  24. e.write();
  25.  
  26. }
Why this is not working?
Last edited by Ancient Dragon; Aug 11th, 2008 at 12:53 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,429
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Writing To a File in Binary Mode

 
0
  #2
Aug 11th, 2008
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
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Writing To a File in Binary Mode

 
0
  #3
Aug 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Writing To a File in Binary Mode

 
0
  #4
Aug 11th, 2008
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.
Last edited by Clockowl; Aug 11th, 2008 at 8:07 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: mksakeesh is an unknown quantity at this point 
Solved Threads: 0
mksakeesh mksakeesh is offline Offline
Newbie Poster

Re: Writing To a File in Binary Mode

 
0
  #5
Aug 11th, 2008
Originally Posted by Clockowl View Post
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:-
  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<string.h>
  4. using namespace std;
  5. class emp
  6. {
  7. public:
  8. char *per;
  9. void write()
  10. {
  11. ofstream out("ss.txt",ios::app | ios::binary);
  12. cout<<"Enter some character ....\n";
  13.  
  14. cin>>*per;
  15. //out.write(per,sizeof(per));
  16. out.write(per,strlen(per));
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. emp e;
  23. e.write();
  24. return 0;
  25. }
Last edited by Ancient Dragon; Aug 11th, 2008 at 12:57 pm. Reason: add code tags -- second time.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,429
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Writing To a File in Binary Mode

 
0
  #6
Aug 11th, 2008
>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.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Writing To a File in Binary Mode

 
0
  #7
Aug 11th, 2008
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.
Last edited by Clockowl; Aug 11th, 2008 at 8:20 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Writing To a File in Binary Mode

 
0
  #8
Aug 11th, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: mksakeesh is an unknown quantity at this point 
Solved Threads: 0
mksakeesh mksakeesh is offline Offline
Newbie Poster

Re: Writing To a File in Binary Mode

 
0
  #9
Aug 12th, 2008
Trying to post again; using code tags
  
  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<string.h>
  4. using namespace std;
  5. class emp
  6. {
  7. public:
  8. char *per;
  9. void write()
  10. {
  11. ofstream out("ss.txt",ios::app | ios::binary);
  12. cout<<"Enter some character ....\n";
  13.  
  14. cin>>*per;
  15. //out.write(per,sizeof(per));
  16. out.write(per,strlen(per));
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. emp e;
  23. e.write();
  24. return 0;
  25. }

I have used char *per because 'write' function only accepts pointer to character.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Writing To a File in Binary Mode

 
0
  #10
Aug 12th, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC