Hi All,
My first post. I have written a code snippet which works on windows. I want it to make run on linux too.
Please help.
Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE *inFile, *outFile;
char * buffer;
size_t inResult;
size_t outResult;
char tempFilePath[L_tmpnam_s];
errno_t err;
tmpfile_s(&outFile);
if( outFile )
{
err = tmpnam_s( tempFilePath, L_tmpnam_s );
if (err)
{
printf("Error occurred creating unique filename.\n");
exit(1);
}
else
{
printf( "%s is safe to use as a temporary file.\n", tempFilePath );
}
fopen_s(&inFile, "test.txt", "rb");
if( !inFile )
{
printf("Error Opening inFile\n");
return 0;
}
// obtain file size:
fseek (inFile , 0 , SEEK_END);
long lSize = ftell (inFile);
rewind (inFile);
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
inResult = fread (buffer, 1, lSize, inFile);
if (inResult != lSize)
{
fputs ("Reading error",stderr);
exit (3);
}
outResult = fwrite( buffer, 1, inResult, outFile );
if (outResult != inResult)
{
fputs ("Reading error",stderr);
exit (3);
}
}
free(buffer);
fclose (inFile);
fclose (outFile);
return 0;
}