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
|
#include <stdlib.h>
#include <stdio.h>
BOOLEAN printError(BOOLEAN condition, char *errorText)
{
if(FALSE==condition)
perror(errorText);
return condition;
}
int main()
{
char* lpBuffer;
FILE* lpInputFile;
FILE* lpOutputFile;
int nSize;
int nRes;
nRes = 1;
/* Open input file */
lpInputFile = fopen("input.txt", "rb");
if (printError(lpInputFile,"Failed to open input file"))
{
/* Open output file */
lpOutputFile = fopen("output.txt", "wb");
if (printError(lpOutputFile,"Failed to open output file"))
{
/* Retrieve size of the file*/
fseek(lpInputFile, 0, SEEK_END);
nSize = ftell(lpInputFile);
fseek(lpInputFile, 0, SEEK_SET);
/* Allocate a buffer to store input file content */
lpBuffer = (char*)malloc(nSize);
if (printError(lpBuffer,"Failed to allocate buffer"))
/* Read from input file to buffer */
if (printError(fread(lpBuffer, 1, nSize, lpInputFile) == nSize,"Failed to read input file"))
/* Write from buffer to output file */
nRes= printError(fwrite(lpBuffer, 1, nSize, lpOutputFile) == nSize,"Failed to write to output file");
free(lpBuffer);
fclose(lpOutputFile);
}
fclose(lpInputFile);
}
return nRes;
} |
Partager