Answers for "fread"

C
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: %zu\n", ret);
    exit(EXIT_FAILURE);
  }

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

  fclose(fp);

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

fread

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Posted by: Guest on April-16-2021
1

fread

//from tutorialspoint.com
#include <stdio.h>
#include <string.h>

int main () {
   FILE *fp;
   char c[] = "this is tutorialspoint";
   char buffer[100];

   /* Open file for both reading and writing */
   fp = fopen("file.txt", "w+");

   /* Write data to the file */
   fwrite(c, strlen(c) + 1, 1, fp);

   /* Seek to the beginning of the file */
   fseek(fp, 0, SEEK_SET);

   /* Read and display data */
   fread(buffer, strlen(c)+1, 1, fp);
   printf("%s\n", buffer);
   fclose(fp);
   
   return(0);
}
Posted by: Guest on April-16-2021

Code answers related to "C"

Browse Popular Code Answers by Language