Need to write an integer to a binary file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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

Need to write an integer to a binary file

 
0
  #1
Aug 19th, 2008
  
  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,433
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: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Need to write an integer to a binary file

 
0
  #2
Aug 19th, 2008
Well.. whats your problem ?
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: Need to write an integer to a binary file

 
0
  #3
Aug 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
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: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need to write an integer to a binary file

 
0
  #4
Aug 19th, 2008
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
  1. int write2()
  2. {
  3. static int counter = 0;
  4. counter++;
  5. out.write(&counter, sizeof(int));
  6. // rest of function here
  7. }
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: Mar 2008
Posts: 1,433
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: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Need to write an integer to a binary file

 
0
  #5
Aug 19th, 2008
try changing the line like this:
  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.
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: Need to write an integer to a binary file

 
0
  #6
Aug 19th, 2008
Can you please explain what a bit bucket is?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,433
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: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Need to write an integer to a binary file

 
0
  #7
Aug 19th, 2008
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.
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: Need to write an integer to a binary file

 
0
  #8
Aug 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
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: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need to write an integer to a binary file

 
0
  #9
Aug 19th, 2008
>>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));
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 2005
Posts: 15,433
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: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Need to write an integer to a binary file

 
0
  #10
Aug 19th, 2008
Originally Posted by mksakeesh View Post
Can you please explain what a bit bucket is?
Click here
Last edited by Ancient Dragon; Aug 19th, 2008 at 8:51 am.
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 is more than three months old.
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