I've just written a C program (Ubuntu platform, using GCC as my compiler) where I have a big chunk of memory, all populated with meaningful data:
u_char* bigChunkOfMem = populateData(); // This works
int memSize = sizeOfChunk( bigChunkOfMem ); // This also works
The trouble is, I need to submit this data to another function, which I did not write. This function takes a File Descriptor as input, so I need to copy my data into a tempfile:
FILE* myTmpFile = tmpfile();
printf("Now copying contents of bigChunkOfMem into myTmpFile!\n");
int i=0;
while( i < memSize ){
fputc( bigChunkOfMem[i], myTmpFile );
printf("...just copied bigChunkOfMem[%d]:%u\n", i, bigChunkOfMem[i]);
i++;
}
printf("\n");
rewind( myTmpFile );
I guess this is working...? My output is this:
Now copying contents of bigChunkOfMem into myTmpFile!
...just copied bigChunkOfMem[0]:101
...just copied bigChunkOfMem[1]:102
...just copied bigChunkOfMem[2]:103
...and so on...
But of course, I submit myTmpFile to the other function, and nothing happens. The other function isn't processing my data.
Its entirely possible my data is incorrectly formatted. But let's set that aside for the moment. In my troubleshooting, it really bothers me that I don't have a way to inspect the tempfile after it is created. I tried using fread():
char[200] buffer;
fread( buffer, memSize+1, 1, myTmpFile );
printf("buffer is:: %s\n", buffer);
rewind( myTmpFile );
But that didn't seem to work. Output was:
buffer is:: [[not my data]]
I assume that a temp file is no logically different than a "regular" file written to disk, i.e., I should be able to inspect the temp file with all the same tools I use for any other file. Is this correct? The online documentation implies this, but I can't find any source that confirms this. Thank you.