c program to write into file from array and read from the same file back into the array

C program to write into file from array and read from the same file back into the array 


#include <stdio.h>
#include <string.h>

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

    /* 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 */ /* if this is not done then read would fail. always do SEEK_SET after fwrite and before performing fread*/

    fseek(fp, 0, SEEK_SET);


    read_buf = fread(buffer, strlen(c)+1, 1, fp);
    if (read_buf <= 0) {
        printf("read failed");
        return 0;
    }

    printf("%s\n", buffer);
    fclose(fp);

    return(0);
}