Wednesday, 15 November 2017
Tuesday, 14 November 2017

C Program to read records from a file and calculate grade of each student and display it
C Program to read records from a file and calculate grade of each student and display it
grade= A if marks>=80
= B if marks>=60 and < 80
= C if marks<60
grade= A if marks>=80
= B if marks>=60 and < 80
= C if marks<60
#include<stdio.h> #include<stdlib.h> int main(void) { struct record { char name[20]; int roll; int marks; }student; FILE *fp; fp = fopen("stu","rb");/*opened in read mode */ if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("\nNAME\t\tMARKS\t\tGRADE\n\n"); while(fread(&student,sizeof(student),1,fp)==1) { printf("%s\t\t",student.name); printf("%4d\t\t",student.marks); if(student.marks>=80) printf("A\n"); else if(student.marks>=60) printf("B\n"); else printf("C\n"); } fclose(fp); return 0; }

C Program to Program to append records to a file
C Program to Program to append records to a file
#include<stdio.h> #include<stdlib.h> int main(void) { struct record { char name[20]; int roll; int marks; }student; FILE *fp; int choice=1; fp=fopen("stu","ab");/*opened in append mode*/ if(fp==NULL) { printf("Error in opening file\n"); exit(1); } while(choice==1) { printf("Enter name : "); scanf("%s",student.name); printf("Enter roll no : "); scanf("%d",&student.roll); printf("Enter marks : "); scanf("%d",&student.marks); fwrite(&student,sizeof(student),1,fp); printf("Want to enter more ?(1 for yes / 0 for no) : "); scanf("%d",&choice); } fclose(fp); return 0; }

C Program to Copy a file to another file
C Program to Copy a file to another file
#include<stdio.h> #include<stdlib.h> int main(void) { FILE *sptr, *dptr; char ch; if((sptr=fopen("source.txt","r"))==NULL) { printf("Error in opening source file\n"); exit(1); } if((dptr=fopen("destination.txt","w"))==NULL) { printf("Error in opening destination file\n"); exit(1); } while((ch=fgetc(sptr))!=EOF) fputc(ch,dptr); fclose(sptr); fclose(dptr); return 0; }
Subscribe to:
Posts (Atom)