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;
}

4.44
9
Digcoal 140 points

                                    //from tutorialspoint.com
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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

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

   /* 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(&quot;%s\n&quot;, buffer);
   fclose(fp);
   
   return(0);
}

4.44 (9 Votes)
0
4.25
8
Manaamana 105 points

                                    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

4.25 (8 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source