Advertisement
Guest User

Untitled

a guest
Jul 4th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main () {
  5.   FILE * pFile;
  6.   long lSize;
  7.   char * buffer;
  8.   size_t result;
  9.  
  10.   pFile = fopen ( "myfile.in" , "rb" );
  11.   if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
  12.  
  13.   // obtain file size:
  14.   fseek (pFile , 0 , SEEK_END);
  15.   lSize = ftell (pFile);
  16.   rewind (pFile);
  17.  
  18.   // allocate memory to contain the whole file:
  19.   buffer = (char*) malloc (sizeof(char)*lSize);
  20.   if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
  21.  
  22.   // copy the file into the buffer:
  23.   result = fread (buffer,1,lSize,pFile);
  24.   if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
  25.   fclose (pFile);
  26.  
  27.   /* the whole file is now loaded in the memory buffer. */
  28.   /* обработку массива buffer писать здесь! */
  29.  
  30.   pFile = fopen ("myfile.out", "wb");
  31.   fwrite (buffer , sizeof(char), lSize, pFile);
  32.   fclose (pFile);
  33.  
  34.   free (buffer);
  35.   return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement