file handling programs.pdf - File Handling Programs 1 Fwrite and fread functions#include<stdio.h>#include<conio.h> struct studentcfw char name[20 int

file handling programs.pdf - File Handling Programs 1...

This preview shows page 1 - 4 out of 10 pages.

File Handling Programs 1.Fwrite and fread functions #include<stdio.h> #include<conio.h> struct student{ char name[20]; int roll; }s; int main() { FILE *fp; fp=fopen("K17.dat","wb"); scanf("%s%d",s.name,&s.roll); fwrite(&s,sizeof(s),1,fp); printf("record saved "); fclose(fp); fp=fopen("K17.dat","rb"); if(fp==NULL) printf("file not present"); else { fread(&s,sizeof(s),1,fp); printf("%s %d",s.name,s.roll); } fclose(fp); }
Background image
2.Fread and fwrite for multiple records #include<stdio.h> #include<conio.h> struct student{ char name[30]; int r; }s[20]; int main() { FILE *fp; int n,i; printf("\nEnter Total No. of students\t"); scanf("%d",&n); fp=fopen("astruct.txt","w"); for(i=0;i<n;i++) { printf("enter name and rollno\n"); scanf("%s%d",s[i].name,&s[i].r); } fwrite(&s, sizeof(s),1,fp); fclose(fp); fp=fopen("astruct.txt","r"); printf("\nData in file is: \n"); fread(&s,sizeof(s),1,fp); for(i=0;i<n;i++) { printf("\n%s",s[i].name); printf("\t%d",s[i].r); } fclose(fp); getch(); } Programs for fgetc() and fputc() 1.Basic writing #include<stdio.h>
Background image
#include<conio.h> main() { char ch; FILE *ptr; ptr=fopen("n.txt","w"); scanf("%c",&ch); fputc(ch,ptr); printf("record saved"); getch(); } 2.Writing Multiple characters in a file #include<stdio.h> #include<conio.h> main() { char ch; FILE *ptr; ptr=fopen("K.txt","w"); printf("enter text and press . to end"); while(ch!='.') {
Background image
Image of page 4

You've reached the end of your free preview.

Want to read all 10 pages?

  • Fall '14
  • gauravtak
  • getch, Cse 101 File Handling Programs, File Handling Programs, fread functions

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes