Well.. whats your problem ?
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
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
int write2()
{
static int counter = 0;
counter++;
out.write(&counter, sizeof(int));
// rest of function here
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
try changing the line like this:
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.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
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.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
>>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));
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Can you please explain what a bit bucket is?
Click here
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
This is writing some unwanted character to the binary file. I have opened the file in Edit Plus.
Of course it does -- afterall that's what abinary file looks like. google for definition of binary file See post #5 above.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343