Advertisement
AZJIO

Прочитать файл

Mar 21st, 2013
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char * FileRead(char *file)
  5. {
  6.     FILE *pFile; long size; char *buffer;
  7.  
  8.     pFile = fopen(file, "r");
  9.     if (pFile != NULL)
  10.     {
  11.         fseek(pFile, 0, SEEK_END);
  12.         size = ftell(pFile);
  13.         fseek(pFile, 0, SEEK_SET);
  14.         if (size != 0)
  15.         {
  16.             //buffer = new char[size + 1];
  17.             buffer = (char*)malloc(size + 1);
  18.             if (fread(buffer, 1, size, pFile) != NULL)
  19.             {
  20.                 buffer[size] = '\0';
  21.                 return buffer;
  22.             }
  23.         }
  24.         fclose(pFile);
  25.     }
  26.     return 0;
  27. }
  28.  
  29. int main(void)
  30. {
  31.     char *pData = FileRead("C:/1.txt");
  32.     if (pData != 0)
  33.     {
  34.         printf(pData);
  35.         getchar();
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement