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

Re: Writing To a File in Binary Mode

 
0
  #11
Aug 13th, 2008
Originally Posted by Ancient Dragon View Post
>>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
  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. per = new char[100];
  14. cin>>per;
  15. //out.write(per,sizeof(per));
  16. out.write(per,strlen(per));
  17.  
  18. }
  19.  
  20. };
  21.  
  22. void main()
  23. {
  24. emp e;
  25. e.write();
  26.  
  27. }
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
  #12
Aug 13th, 2008
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).
Last edited by Clockowl; Aug 13th, 2008 at 11:44 am.
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