| | |
Need to write an integer to a binary file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<fstream.h> #include<iostream.h> #include<string.h> using namespace std; class emp { public: char *per; int i; int write2() { ofstream out("ss.txt",ios::app | ios::binary ); cout<<"Enter some character ....\n"; per = new char[100]; cin.getline(per,100,'\n') out.write("\n",1); out.write(per,strlen(per)); out<< "\n"; per = "asdfgh"; out.write(per,strlen(per)); out.write((char *)&i,5); out.write(per,strlen(per)); return 0; } int read_2() { char * buffer; long size; ifstream in("ss.txt",ios::in|ios::binary|ios::ate);//try without any of these flags. size = in.tellg(); in.seekg (0, ios::beg); buffer = new char [size]; in.read (buffer, size); in.close(); cout << "the complete file is in a buffer...\n"; cout<<buffer; delete[] buffer; return 0; } }; void main() { emp e; e.write2();//Writes to a file in binary format even if the input data has space. eg:- e.read_2();//Reads 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
>>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)
int write2() { static int counter = 0; counter++; out.write(&counter, sizeof(int)); // rest of function here }
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.
•
•
Join Date: Mar 2008
Posts: 1,433
Reputation:
Solved Threads: 118
try changing the line like this:
That should save the integer, but this saves it in the form of binary, so its not readable if you open it in notepad.
C++ Syntax (Toggle Plain Text)
out.write(reinterpret_cast<char*>( &i ) , sizeof i);
•
•
Join Date: Mar 2008
Posts: 1,433
Reputation:
Solved Threads: 118
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.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
>>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(&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.
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.
![]() |
Similar Threads
- Help for file handling in VB. NET (VB.NET)
- Need help with binary tree program. (C)
- Reading or writing a double into a bin file (C++)
- What errors can occur while reading a file? (C)
- Need help with Binary files and data types in VB6 (Visual Basic 4 / 5 / 6)
- Extract header info from image file (Python)
- Converting byte value into integer and vice versa (C++)
Other Threads in the C++ Forum
- Previous Thread: geany and tags
- Next Thread: Matrix - Matrix multiplication
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






