Wisdom Materials
Home
About Us
Our Clients
Careers
Services
Education
Jobs
News
Business
Health
Astrology
Entertainment
RealEstate
Devotion
Contact Us
CPP Programs
/ CPP Program Crud operations on student record using files
Program Name
CPP Program Crud operations on student record using files
Program Code
Copy Program
#include
int avlrollno(int rno); struct student { int rollno; char name[30]; float mark; }stud; void insert() { FILE *fp; fp = fopen("Record.txt", "a"); printf("Enter the Roll no :"); scanf("%d", &stud.rollno); printf("Enter the Name :"); scanf("%s", &stud.name); printf("Enter the mark :"); scanf("%f", &stud.mark); fwrite(&stud, sizeof(stud), 1, fp); fclose(fp); } void display() { FILE *fp1; fp1 = fopen("Record.txt", "r"); printf("\nRoll Number\tName\tMark\n\n"); while (fread(&stud, sizeof(stud), 1, fp1)) printf(" %d\t\t%s\t%.2f\n", stud.rollno, stud.name, stud.mark); fclose(fp1); } void search() { FILE *fp2; int r, s, avl; printf("\nEnter Roll no to search :"); scanf("%d", &r); avl = avlrollno(r); if (avl == 0) printf("Roll No %d is not available in the file\n",r); else { fp2 = fopen("Record.txt", "r"); while (fread(&stud, sizeof(stud), 1, fp2)) { s = stud.rollno; if (s == r) { printf("\nRoll no = %d", stud.rollno); printf("\nName = %s", stud.name); printf("\nMark = %.2f\n", stud.mark); } } fclose(fp2); } } void deletefile() { FILE *fpo; FILE *fpt; int r, s; printf("Enter Roll no to delete the record:"); scanf("%d", &r); if (avlrollno(r) == 0) printf("Roll no %d is not available in the file\n", r); else { fpo = fopen("Record.txt", "r"); fpt = fopen("TempFile", "w"); while (fread(&stud, sizeof(stud), 1, fpo)) { s = stud.rollno; if (s != r) fwrite(&stud, sizeof(stud), 1, fpt); } fclose(fpo); fclose(fpt); fpo = fopen("Record.txt", "w"); fpt = fopen("TempFile", "r"); while (fread(&stud, sizeof(stud), 1, fpt)) fwrite(&stud, sizeof(stud), 1, fpo); printf("\nRecord.txt DELETED\n"); fclose(fpo); fclose(fpt); } } void update() { int avl; FILE *fpt; FILE *fpo; int s, r, ch; printf("Enter roll number to update a record:"); scanf("%d", &r); avl = avlrollno(r); if (avl == 0) { printf("Roll number %d is not Available in the file", r); } else { fpo = fopen("Record.txt", "r"); fpt = fopen("TempFile", "w"); while (fread(&stud, sizeof(stud), 1, fpo)) { s = stud.rollno; if (s != r) fwrite(&stud, sizeof(stud), 1, fpt); else { printf("\n\t1. Update Name of Roll Number %d", r); printf("\n\t2. Update Mark of Roll Number %d", r); printf("\n\t3. Update both Name and Mark of Roll Number %d", r); printf("\nEnter your choice:"); scanf("%d", &ch); switch (ch) { case 1: printf("Enter Name:"); scanf("%s", &stud.name); break; case 2: printf("Enter Mark : "); scanf("%f", &stud.mark); break; case 3: printf("Enter Name: "); scanf("%s", &stud.name); printf("Enter Mark: "); scanf("%f", &stud.mark); break; default: printf("Invalid Selection"); break; } fwrite(&stud, sizeof(stud), 1, fpt); } } fclose(fpo); fclose(fpt); fpo = fopen("Record.txt", "w"); fpt = fopen("TempFile", "r"); while (fread(&stud, sizeof(stud), 1, fpt)) { fwrite(&stud, sizeof(stud), 1, fpo); } fclose(fpo); fclose(fpt); printf("Record.txt UPDATED"); } } void sort() { int a[20], count = 0, i, j, t; FILE *fpo; fpo = fopen("Record.txt", "r"); while (fread(&stud, sizeof(stud), 1, fpo)) { a[count] = stud.rollno; count++; } for (i = 0; i
a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } } printf("Roll No.\tName\t\tMark\n\n"); for (i = 0; i
Input
Output
Home
Back