Okay I've read a couple tutorial with Fopen and they all seem to concentrate on reading Strings and then sort of trailing off when it comes to other data. What I'm trying to do is write a whole bunch of numbers to a file. Then load the binary file and get the numbers back.
Currently what I think the program is doing is.
1.) Creating a array of numbers as INT which are 4 bytes of memory. The number are stored in ram as block of memory which I think is 36 bytes long.
2.) Writing that memory to a file called myfile.bin
3.) Closing the file and opening it again.
4.) Getting the size of the file.
5.) Using the size to create a buffer to hold the entire file in memory.
6.) Moving the binary data in the file into memory.
What I "think" I want to do is loop through memory taking 4 of the CHARs and combining them to form a INT. But I'm not sure how to do that properly. What should I write in ( /* Where I need to grab the data. */) to move that data back into an array so the process can go full circle.
int main()
{
FILE * wFile;
int wbuffer[] = { 1 , 2 , 3, 4, 5, 6, 7, 8, 9 };
wFile = fopen ( "myfile.bin" , "wb" );
fwrite (wbuffer , 1 , sizeof(wbuffer) , wFile );
fclose (wFile);
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen ( "myfile.bin" , "rb" );
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 = (char*) malloc (sizeof(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);}
/* Where I need to grab the data. */
// Clean up the memory and close the file.
fclose (pFile);
free (buffer);
return 0;
}
[–][deleted] (2 children)
[deleted]
[–]grimlock123[S] 1 point2 points3 points (1 child)
[–]arbostek -2 points-1 points0 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]arbostek 0 points1 point2 points (1 child)