943,779 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2901
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 13th, 2008
0

Re: Writing To a File in Binary Mode

>>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
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 13th, 2008
0

Re: Writing To a File in Binary Mode

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to count the number of charachters a user has inputted?
Next Thread in C++ Forum Timeline: Need help on a sorting algorithm function.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC