EXAMPLES OF FILE I/O

WAP to print / write rno,sname,cname in student.txt file.

#include<stdio.h>

void main()

{

  int rno;

  char sname[100],cname[100];

  FILE *fp;

  fp=fopen(“student.txt”, “w”);

  if( fp==NULL )

{

  printf(“file error”);

  exit(0);

}

  printf(“Enter rno,sname, cname”);

  scanf(“%d%s%s”,&rno,&sname,&cname);

  fprintf(fp,”%d %s %s”,rno,sname,cname);

  fclose( fp );

  printf(“Data successfully write to student.txt file”);

}   

 

WAP to read rno,sname,cname from student.txt file.

 

#include<stdio.h>

void main()

{

  int rno;

  char sname[100],cname[100];

  FILE *fp;

  fp=fopen(“student.txt”, “r”);

  if( fp==NULL )

{

  printf(“File error”);

  exit(0);

}

  fscanf(fp,”%d%s%s”,&rno,&sname,&cname);

  printf(“Rno=%d”,rno);

  printf(“\nSname=%s”,sname);

  printf(“\nCname=%s”,cname);

  fclose(fp);

Output :

Rno=501

Sname=santosh

Cname=ECE

WAP to read entire information of a file.

 

#include<stdio.h>

void main()

{

  char ch;

  FILE *fp;

  fp=fopen(“student.txt”, “r”);

  if( fp==NULL )

{

  printf(“File error”);

  exit(0);

}

  while( !feof(fp))

{

  fscanf(fp, “%c”,&ch);

  if( feof(fp))

  break;

  printf(“%c”,ch);

}

  fclose( fp );

}   

Output :

501 santosh ECE

 

WAP to copy one file to another file.

 

#include<stdio.h>

void main()

{

  char ch;

  FILE *fp1,*fp2;

  fp1=fopen(“student.txt”, “r”);

  fp2=fopen(“dup.txt”, “w”);

  if( fp1==NULL )

{

  printf(“File 1 error”);

  exit(0);

}

  if( fp2==NULL )

{

  printf(“File 2 error”);

  exit(0);

}

  while( !feof( fp1 ))

{

  fscanf(fp1, “%c”,&ch);

  fprintf(fp2, “%c”,ch);

}

  printf(“Student.txt copied to dup.txt”);

  fclose(fp1);

  fclose(fp2);

}  

Output:

Student.txt copied to dup.txt

 

WAP to merge 2 files.

 

#include<stdio.h>

void main()

{

  char ch;

  FILE *fp1,*fp2,*fp3;

  fp1=fopen(“Student.txt”, “r”);

  fp2=fopen(“Employee.txt”, “r”);

  fp3=fopen(“Merged.txt”, “w”);

  if( fp1==NULL )

{

  printf(“File 1 error”);

  exit(0);

}

  if( fp2==NULL )

{

  printf(“File 2 Error”);

  exit(0);

}

  if( fp3==NULL )

{

  printf(“File 3 error”);

  exit(0);

}

  while( !feof(fp1))

{

  fscanf(fp1, “%c”,&ch);

  fprintf(fp3, “%c”,ch);

}

  while( !feof(fp2))

{

  fscanf(fp2, “%c”,&ch);

  fprintf(fp3, “%c”,ch);

}

  fclose(fp1 );

  fclose(fp2);

  fclose(fp3);

  printf(“Student.txt and Employee.txt are merged into Merged.txt”);

}  

Output:

Student.txt and Employee.txt are merged into Merged.txt

 

WAP to separated vowels and consonants of a student.txt file.

 

#include<stdio.h>

void main()

{

  char ch;

  FILE *fp1,*fp2,*fp3;

  fp1=fopen("Student.txt",”r”);

  fp2=fopen(“Vowels.txt”,”w”);

  fp3=fopen(“Consonants.txt”,”w”);

  if( fp1==NULL )

{

  printf(“File 1 error”);

  exit(0);

}

  if( fp2==NULL )

{

  printf(“File 2 Error”);

  exit(0);

}

  if( fp3==NULL )

{

  printf(“File 3 error”);

  exit(0);

}

  while( !feof(fp1))

{

  fscanf(fp1,“%c”,&ch);

  if( ch=='a' || ch=='e' ||ch=='i' ||ch=='o'||ch=='u' || ch=='A'   ||ch=='E'||ch=='I'||ch=='O'||ch=='U')

  fprintf(fp2“%c”,ch);

  else

  fprintf(fp3,“%c”,ch);

}

  fclose(fp1);

  fclose(fp2);

  fclose(fp3);

  printf(“data seperated”);

}  

Output:

data seperated