Read files with fread in C language

htt‮w//:sp‬ww.lautturi.com
Read files with fread in C language
/*
Example: read and write file using fread/fwrite in C language
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>

int main () {
   FILE *fp;
   char c[] = "data lautturi data";
   char buffer[100];

   // Open file in read-write mode
   fp = fopen("file.txt", "w+");

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

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

   /* Read and print the text */
   fread(buffer, strlen(c)+1, 1, fp);
   printf("%s\n", buffer);
   fclose(fp);
   
   return(0);
}
Created Time:2017-08-28 18:58:46  Author:lautturi