Write static data in file and read from file - C Language Programming

Program in C Language to write static data in file and read from file



Click here to open this program in Turbo C++

/**********************************************************
 Statement - Write static data in file and read from file
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************************/


#include<stdio.h>
#include<conio.h>

struct Student {
    int roll;
    char name[12];
    int percent;
} s1 = { 96, "VINEET", 76 };

void main() {
    FILE *fp;
    struct Student s2;
    clrscr();
    
    //Write details of s1 to file
    fp = fopen("ip.txt", "w");
    fwrite(&s1, sizeof(s1), 1, fp);
    fclose(fp);
    
    //Read saved details from file
    fp = fopen("ip.txt", "r");
    fread(&s2, sizeof(s2), 1, fp);
    fclose(fp);
    
    printf("\nRoll : %d", s2.roll);
    printf("\nName : %s", s2.name);
    printf("\nPercent : %d", s2.percent);
    
    getch();
}
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.