File Handling in C
File Handling in C
Qno2) WAP to read characters from file named “myfile.txt” and display
it to monitor.
Ans:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(){
char ch;
FILE *fp;
fp=fopen("myfile.txt","r");
if(fp==NULL){
printf("file couldn't be opened");
getch();
exit(0);
}
printf("The characters from file are:\n");
while((ch=fgetc(fp))!=EOF){
putch(ch);
}
fclose(fp);
getch();
return 0;
}
Qno3)WAP to read characters from keyboard,store it in a file named
“myfile.dat” and display it.
Ans:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(){
char ch;
FILE *fp;
fp=fopen("myfile.dat","wb+");
if(fp==NULL){
printf("file couldn't be opened");
getch();
exit(0);
}
printf("Enter some characters");
while((ch=getchar())!='\n'){
fputc(ch,fp);
}
rewind(fp);
printf("characters from the file are:\n");
while((ch=fgetc(fp))!=EOF){
putch(ch);
}
fclose(fp);
getch();
return 0;
}
}
Qno2)WAP to input name,address,registration number,faculty and
academic year of admission in university of ‘n’ number of students of PU
and append them in a data file called “STUDENT.DAT” .Then display
records of those students by reading the records from “STUDENT.DAT”
data file who got admission in 2016.[PU. 2015 Spring]
Ans:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char name[100];
char address[100];
long int reno;
char faculty[100];
float year;
};
int main(){
struct student s[100];
int i,n;
FILE *fp;
fp=fopen("STUDENT.DAT","ab+");
if(fp==NULL){
printf("file cannot be opened");
getch();
exit(0);
}
printf("enter total number of students");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter name");
fflush(stdin);
gets(s[i].name);
printf("Enter address");
gets(s[i].address);
printf("Enter registration number");
scanf("%ld",&s[i].reno);
printf("Enter faculty");
fflush(stdin);
gets(s[i].faculty);
printf("Enter academic year");
scanf("%d",&s[i].year);
fwrite(&s,sizeof(s),1,fp);
}
rewind(fp);
for(i=0;i<n;i++){
fread(&s,sizeof(s),1,fp);
if(s[i].year==2016)
{
printf("name=%s,address=%s,faculty=%s,registration
number=%ld and academic
year=%d",s[i].name,s[i].address,s[i].faculty,s[i].reno,s[i].year);
}
}
fclose(fp);
getch();
return 0;
}
Qno3)WAP to read the name,author and price of 500 books in a library
from “library.dat”.Now print the book name and price of those books
whose price is above Rs.300.[PU. 2015 Fall]
Ans:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char name[100];
char author[100];
float price;
};
int main(){
struct student s[500];
int i;
FILE *fp;
fp=fopen("library.dat","wb+");
if(fp==NULL){
printf("file cannot be opened");
getch();
exit(0);
}
for(i=0;i<500;i++){
printf("Enter name");
fflush(stdin);
gets(s[i].name);
printf("Enter author");
gets(s[i].author);
printf("Enter price of the book");
scanf("%f",&s[i].price);
fwrite(&s,sizeof(s),1,fp);
}
rewind(fp);
for(i=0;i<500;i++){
fread(&s,sizeof(s),1,fp);
if(s[i].price>300)
{
}
Qno4)WAP to open a new file,read name,address and telephone
number of 10 employees from the user and write it to a file.
Ans:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct employee{
char name[100];
char address[100];
long int telno;
};
int main(){
struct employee e[10];
int i;
FILE *fp;
fp=fopen("employee.dat","wb");
if(fp==NULL){
printf("File cannot be opened");
getch();
exit(0);
}
for(i=0;i<10;i++){
printf("enter employee name");
gets(e[i].name);
printf("enter address");
gets(e[i].address);
printf("enter telephone number");
scanf("%ld",&e[i].telno);
fwrite(&e,sizeof(e),1,fp);
}
fclose(fp);
getch();
}