fread

Solutions on MaxInterview for fread by the best coders in the world

showing results for - "fread"
Andrea
23 Nov 2020
1// from the linux programmer's manual, fread(3)
2#include <stdio.h>
3#include <stdlib.h>
4
5int main()
6{
7  FILE *fp = fopen("/bin/sh", "rb");
8  if (!fp) {
9    perror("fopen");
10    return EXIT_FAILURE;
11  }
12
13  unsigned char buffer[4];
14
15  size_t ret = fread(buffer, 4, 1, fp);
16  if (ret != sizeof(*buffer)) {
17    fprintf(stderr, "fread() failed: %zu\n", ret);
18    exit(EXIT_FAILURE);
19  }
20
21  printf("ELF magic: %#04x%02x%02x%02x\n", buffer[0], buffer[1],
22         buffer[2], buffer[3]);
23
24  fclose(fp);
25
26  return 0;
27}
28
Ray
14 Jul 2019
1size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Cristina
22 May 2017
1//from tutorialspoint.com
2#include <stdio.h>
3#include <string.h>
4
5int main () {
6   FILE *fp;
7   char c[] = "this is tutorialspoint";
8   char buffer[100];
9
10   /* Open file for both reading and writing */
11   fp = fopen("file.txt", "w+");
12
13   /* Write data to the file */
14   fwrite(c, strlen(c) + 1, 1, fp);
15
16   /* Seek to the beginning of the file */
17   fseek(fp, 0, SEEK_SET);
18
19   /* Read and display data */
20   fread(buffer, strlen(c)+1, 1, fp);
21   printf("%s\n", buffer);
22   fclose(fp);
23   
24   return(0);
25}
similar questions
fread condition