https://www.bybit.com/register?affiliate_id=38000&group_id=52135&group_type=1
[856x508] Get $90 Rewards

C Programming File Handling | Read, Display & Search in *files

Write a Modular C Programming code for Reading, Displaying, and operations on files*

*The file should be saved in (student.txt) this Format in the location of this c file.
Rno   Name    CGPA
1     ABC      7
2     PQR      9
3     MNO      8
4     DEF      10

It will Display all student record who have got CGPA >= 8.5

Refer To Notes:  Notes: File Handling in C programming | Operations in Files

CODE:

#include<stdio.h>
#include<stdlib.h>
struct student
{
    int rno;
    char name[20];
    double cgpa;
};
void display(FILE *);
int main()
{
    FILE *fp1;
    fp1 = fopen("student.txt","r");
    if(fp1==NULL)
    {
        perror("");
        exit(0);
    }
    display(fp1);
    fclose(fp1);
    return 0;
}
void display(FILE *fp)
{
    struct student s;
    while(fscanf(fp,"%d%s%lf",&s.rno,s.name,&s.cgpa)==3)
    {
        if(s.cgpa>=8.5)
        {
            printf("%d\t%s\t%lf\n",s.rno,s.name,s.cgpa);
        }
    }
}



OUTPUT

2 PQR 9 
4 DEF 10

Process returned 0 (0x0)   execution time : 369.409 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, and MISC on the below page:

Top 100+ C Programming codes – KLE Technological University

 

 

Leave a Comment