Heap Corruption Error help
Im making a program that will take a input file specified on the command line through argv[1] and a output file also specified on the command line argv[2], basically im just printing the information from the input file to the output file in ascii format zero padded, Im using fread to read the entire file into a dynamically allocated block of memory, I use fseek and ftell to get the size of the input file, and then use a loop to print the values to the output file, but somehow when I increment the pointer to move to the next byte in the allocated memory space. it gives me the _CrtIsValidHeapPointer(pUserData), Debug Assertion Error!
Here is the following code for my program, please help me out in anyway possible, thank you! It still is correctly writing to the output file the values from the input file, but it's giving me that error.
Code:
#include
<stdio.h>
#include
<stdlib.h>
int
main(int argc,char* argv[])
{
int byteCount=1;
char* memorySpace;
char* memoryPosition;
long lSize;
char buffer[300];
FILE* ipf;
FILE* opf;
ipf=fopen(argv[1],
"r");
opf=fopen(argv[2],
"w");
fseek( ipf,0, SEEK_END );
lSize=ftell(ipf);
rewind(ipf);
memorySpace=(
char*)malloc(lSize);
fread(memorySpace,1,lSize,ipf);
while (byteCount!=lSize)
{
fprintf(opf,
"0%d ",*memorySpace);
++byteCount;
++memorySpace;
if(byteCount%10==0)
{
fprintf(opf,
"\n");
}
}
fclose(ipf);
fclose(opf);
free(memorySpace);
return 0;
}