Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* fread example: read a complete file */
- #include "iostream"
- #include <stdio.h>
- #include <stdlib.h>
- int main () {
- FILE * pFile;
- long lSize;
- char * buffer;
- size_t result;
- pFile = fopen ( "new.txt" , "rb" );
- if (pFile==NULL) {fputs ("File error\n",stderr); exit (1);}
- std::cout << pFile << std::endl;
- // obtain file size:
- fseek (pFile , 0 , SEEK_END);
- lSize = ftell (pFile);
- rewind (pFile);
- // allocate memory to contain the whole file:
- buffer = (char*) malloc (sizeof(char)*lSize);
- if (buffer == NULL) {fputs ("Memory error\n",stderr); exit (2);}
- // copy the file into the buffer:
- result = fread (buffer,1,lSize,pFile);
- if (result != lSize) {fputs ("Reading error\n",stderr); exit (3);}
- /* the whole file is now loaded in the memory buffer. */
- std::cout << pFile << "\n" << lSize << "\n" << buffer << std::endl;
- // terminate
- fclose (pFile);
- free (buffer);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement