problems with writing binary file to .txt
so i have a problem writing this binary file "input.raw" to a text file "myFile.txt" my output just looks like this
the "input.raw" file is supposebly 600000 bytes.
my code thus far looks like this, this is obviously written in C language and not C++ hope that is not a problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void readFile();
int main ()
{
readFile();
}
void(readFile())
{
FILE * pFile;
pFile = fopen("input.raw" , "rb");
long lSize;
unsigned char * buffer;
size_t result;
if (pFile==NULL)
{
fputs ("File error",stderr); exit (1);
}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (unsigned char*) malloc (sizeof(unsigned char)*lSize);
if (buffer == NULL)
{
fputs ("Memory error",stderr); exit (2);
}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize)
{
fputs ("Reading error",stderr); exit (3);
}
/* the whole file is now loaded in the memory buffer. */
FILE * pFile1;
pFile1 = fopen ( "myfile.txt" , "wb" );
fwrite (buffer , 1 , sizeof(buffer) , pFile1 );
fclose (pFile1);
// terminate
fclose (pFile);
}
|
Topic archived. No new replies allowed.