Answers for "fread c++"

C
1

cpp fread

/* fread example: read an entire file */
#include <stdio.h>
#include <stdlib.h>

int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "myfile.bin" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  // 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",stderr); exit (2);}

  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */

  // terminate
  fclose (pFile);
  free (buffer);
  return 0;
}
Posted by: Guest on April-08-2021
3

fread

// from the linux programmer's manual, fread(3)
#include <stdio.h>
#include <stdlib.h>

int main()
{
  FILE *fp = fopen("/bin/sh", "rb");
  if (!fp) {
    perror("fopen");
    return EXIT_FAILURE;
  }

  unsigned char buffer[4];

  size_t ret = fread(buffer, 4, 1, fp);
  if (ret != sizeof(*buffer)) {
    fprintf(stderr, "fread() failed: %zun", ret);
    exit(EXIT_FAILURE);
  }

  printf("ELF magic: %#04x%02x%02x%02xn", buffer[0], buffer[1],
         buffer[2], buffer[3]);

  fclose(fp);

  return 0;
}
Posted by: Guest on December-16-2020
0

cpp fread

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
Posted by: Guest on April-08-2021

Code answers related to "C"

Browse Popular Code Answers by Language