943,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 126
  • C++ RSS
Sep 2nd, 2010
0

writing into binary file

Expand Post »
I have a problem when i am writing a content into a files as a binary format.I noticed, that it writes the content two times into the file.
Here is the code.


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. using namespace std;
  5. class store
  6. {
  7. protected:
  8. char pass[50];
  9. public:
  10. fstream file;
  11. store();
  12. ~store();
  13. void writing();
  14. void reading();
  15. };
  16. store::store()
  17. {
  18. file.open("binary.dat",ios::in | ios::out | ios::app | ios::binary);
  19. }
  20. store::~store();
  21. {
  22. file.close();
  23. }
  24. int main()
  25. {
  26. store b;
  27. b.writing();
  28. b.reading();
  29. return 0;
  30. }
  31. void store::writing()
  32. {
  33. char buf[] = "hello";
  34. strcpy_s(pass,sizeof(buf),buf);
  35. file.write(reinterpret_cast<char*>(&pass),sizeof(pass));
  36. }
  37. void store::reading()
  38. {
  39. file.seekp(0,ios_base::beg);
  40. while(!file.eof())
  41. {
  42. file.read(reinterpret_cast<char*>(&pass),sizeof(pass));
  43. cout<<pass;
  44. }
  45. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dineshcbe is offline Offline
12 posts
since Sep 2010
Sep 2nd, 2010
0
Re: writing into binary file
>it writes the content two times into the file.
Close. It writes the content once, but because of a common bug you read it twice.

>while(!file.eof())
This is the bug. eof only returns true after you've tried and failed to read from the file. Using it as a loop condition without any other tests in the body is a fencepost error.

Here are two common solutions. If your reading function returns a stream state, you can use it directly as the condition:
C++ Syntax (Toggle Plain Text)
  1. while (file.read(pass, sizeof pass))
  2. cout<< pass;
Or you can double up the eof test and avoid processing after a failed read:
C++ Syntax (Toggle Plain Text)
  1. while (!file.eof()) {
  2. file.read(pass, sizeof pass);
  3.  
  4. if (!file.eof())
  5. cout<< pass;
  6. }
Obviously the former is preferred because it's shorter, cleaner, and generally perceived as easier to follow.
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: Create a music/audio player
Next Thread in C++ Forum Timeline: Clear Text





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


Follow us on Twitter


© 2011 DaniWeb® LLC