943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3447
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 19th, 2008
0

Need to write an integer to a binary file

Expand Post »
  
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. int i;
  10. int write2()
  11. {
  12. ofstream out("ss.txt",ios::app | ios::binary );
  13.  
  14. cout<<"Enter some character ....\n";
  15.  
  16. per = new char[100];
  17.  
  18. cin.getline(per,100,'\n')
  19.  
  20. out.write("\n",1);
  21.  
  22. out.write(per,strlen(per));
  23.  
  24. out<< "\n";
  25.  
  26. per = "asdfgh";
  27.  
  28. out.write(per,strlen(per));
  29.  
  30. out.write((char *)&i,5);
  31.  
  32. out.write(per,strlen(per));
  33.  
  34. return 0;
  35. }
  36. int read_2()
  37. {
  38. char * buffer;
  39.  
  40. long size;
  41.  
  42. ifstream in("ss.txt",ios::in|ios::binary|ios::ate);//try without any of these flags.
  43.  
  44. size = in.tellg();
  45.  
  46. in.seekg (0, ios::beg);
  47.  
  48. buffer = new char [size];
  49.  
  50. in.read (buffer, size);
  51.  
  52. in.close();
  53.  
  54. cout << "the complete file is in a buffer...\n";
  55.  
  56. cout<<buffer;
  57.  
  58. delete[] buffer;
  59.  
  60. return 0;
  61. }
  62.  
  63.  
  64. };
  65.  
  66. void main()
  67. {
  68. emp e;
  69. e.write2();//Writes to a file in binary format even if the input data has space. eg:-
  70. e.read_2();//Reads a binary file.
  71. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

Well.. whats your problem ?
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

I am not able to perform the step "out.write((char *)&i,5);", I am trying to log the count for each time we call the write2() function, but i am not able to enter integer value to a binary file.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

line 26: that just caused a memory leak. Where did the memory allocated by new operator on line 16 go??? Answer: the Bit Bucket. You have to call delete[] before reusing that pointer on line 26.

>>I am not able to perform the step "out.write((char *)&i,5);",
Suggestion: Create a static counter at the top of the write2() function, increment it, then write it. That way it will not have to depend on something else writing it
C++ Syntax (Toggle Plain Text)
  1. int write2()
  2. {
  3. static int counter = 0;
  4. counter++;
  5. out.write(&counter, sizeof(int));
  6. // rest of function here
  7. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

try changing the line like this:
C++ Syntax (Toggle Plain Text)
  1. out.write(reinterpret_cast<char*>( &i ) , sizeof i);
That should save the integer, but this saves it in the form of binary, so its not readable if you open it in notepad.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

Can you please explain what a bit bucket is?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

Any memory that you allocate should be deleted using the delete keyword otherwise it will case a memory leak. Even though AD has already said it, on line 26 you have assigned the pointer per to point at the characters "asdfgh" and you have ditched the 100 characters that you allocated on line 16. If you want to fill that array with a string, use strcpy.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

Suggestion of AD has produced compilation error as:-
Error E2034 binaryfilewriting.cpp 64: Cannot convert 'int *' to 'const char *' i
n function emp::write2()
Error E2342 binaryfilewriting.cpp 64: Type mismatch in parameter 's' (wanted 'co
nst char *', got 'int *') in function emp::write2()
*** 2 errors in Compile ***

line 64 here is "out.write(&i, sizeof(i));"

and trying with "out.write(reinterpret_cast<char*>( &i ) , sizeof i);", has run well, but when file is opened it is showing some unwanted characters. I have opened the file in editplus.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

>>Error E2034 binaryfilewriting.cpp 64: Cannot convert 'int *' to 'const char *' i

>> out.write(&counter, sizeof(int));
That should have been typecast, like this:
out.write((char*)&counter, sizeof(int));
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 19th, 2008
0

Re: Need to write an integer to a binary file

Click to Expand / Collapse  Quote originally posted by mksakeesh ...
Can you please explain what a bit bucket is?
Click here
Last edited by Ancient Dragon; Aug 19th, 2008 at 8:51 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

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: geany and tags
Next Thread in C++ Forum Timeline: Matrix - Matrix multiplication





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


Follow us on Twitter


© 2011 DaniWeb® LLC