Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,
I have been trying this and have not been able to resolve it, can someone please direct me to an example or tell me what I am doing wrong.

////////
I am processing some data...  getting a value to Count.
long ArraySize = Count;
BYTE *FileOut;
FileOut = new BYTE[ArraySize];
///
I am able to put my data into file out.
///

I am having issues trying to write FileOut to a binary file.


FILE *file2 = NULL;
if ((file2 = fopen(FileName, "ab")) == NULL)
{
	Temp.Format(_T("Could not open specified file"));
}
else
{
	fwrite(FileOut, sizeof(FileOut[0]), sizeof(FileOut)/ sizeof(FileOut[0]),file2);
}
delete[]FileOut;
fclose(file2);


Any help will be appreciated.


Sorry I am not getting the full array out only 4 numbers.
Posted
Updated 31-Jan-14 17:19pm
v2
Comments
H.Brydon 31-Jan-14 23:00pm    
You didn't say what kind of problems you are having with it... more detail?

1 solution

Your debugger is for this type of problem.

When you take sizeof(FileOut) you get the size of pointer FileOut which is 4 bytes.

Change this line to:

C++
fwrite(FileOut, sizeof(BYTE), sizeof(BYTE) * ArraySize, file2);


[Edit]

There is a correction to the above. It should read:
C++
fwrite(FileOut, sizeof(BYTE), ArraySize, file2);


It will then work correctly if FileOut is declared as int for example:

C++
fwrite(FileOut, sizeof(int), ArraySize, file2);
 
Share this answer
 
v2
Comments
H.Brydon 1-Feb-14 23:35pm    
I would recommend the slightly different:

fwrite(FileOut, sizeof(FileOut[0]), ArraySize, file2);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900