841_FileHandling
841_FileHandling
XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU, NEPAL
Phone: 01-5321365, 01-5344636
Email: [email protected]
“C PROGRAMMING -
FILE HANDLING”
1. Write a C program to create a new data file and store a character in it using putc(). Read the character
afterwards using getc(). ................................................................................................................. 1
2. Write a C program to create a new data file and store an integer in it using putw(). Read the integer
afterwards using getw(). ................................................................................................................ 2
3. Write a C program to create a new data file and store a string in it using fputs(). Read the ................. 3
string afterwards using fgets(). ....................................................................................................... 3
4. Write a C program to create a new data file and store a sentence “I love C programming.” using
fwrite(). Read the file afterwards using fread() upto 8 characters only. ................................................ 4
5. Write a C program to store Name, Address, Age and Salary of 10 employees in a data file
STAFF.TXT. ............................................................................................................................... 6
6. Write a C program to display the records whose salary is greater than 15000 and who is younger than
25 from the data file STAFF.TXT created in Q5. Display the records in proper .................................... 9
format. ........................................................................................................................................ 9
QSN 7) Write a C program to add two new records of employees in STAFF.TXT created in Q5. .........10
8. Write a C program to read the STAFF.TXT file created in Q5 and save the records of employees with
Address “kathmandu” in a new KATHMANDU.TXT file. ...............................................................12
9. Write a C program to display the records of the KATHMANDU.TXT file created in Q8 and rename it
with NEWKATHMANDU.TXT filename. .....................................................................................14
10) Write a C program to delete the file NEWKATHMANDU.TXT from Q9. ....................................15
ii
1. Write a C program to create a new data file and store a character in it using
putc(). Read the character afterwards using getc().
#include <stdio.h>
#include <stdlib.h>
int main(){
char c,r;
printf("Program executed by Rejina Bhattarai.\n");
FILE *fp;
fp = fopen("reju's_code.txt","w+");
if(fp == NULL){
printf("Error 404!!");
exit(1);
}
putc(c,fp);
rewind(fp);
r = getc(fp);
fclose(fp);
printf("character read from file is: %c",r);
return 0;
}
1
2. Write a C program to create a new data file and store an integer in it using
putw(). Read the integer afterwards using getw().
#include <stdio.h>
#include <stdlib.h>
int main(){
int c,r;
printf("Program executed by Rejina Bhattarai.\n");
FILE *fp;
fp = fopen("reju's_code.txt","w+");
if(fp == NULL){
printf("Error 404!!");
exit(1);
}
putw(c,fp);
rewind(fp);
r = getw(fp);
fclose(fp);
printf("character read from file is: %d",r);
return 0;
}
2
(cause integers are stored in binary.)
3. Write a C program to create a new data file and store a string in it using fputs().
Read the
#include<stdio.h>
#include<stdlib.h>
int main() {
char str[20];
printf("Program Run By Rejina Bhattarai.\n");
FILE *fp;
fp = fopen("Reju's_code.txt", "w+");
if(fp == NULL) {
printf("Error: 404!!");
}
3
printf("Enter a string: ");
scanf("%20[^\n]", str);
fputs(str, fp);
rewind(fp);
char returnedStr[20];
fgets(returnedStr, 20, fp);
4. Write a C program to create a new data file and store a sentence “I love C
programming.” using fwrite(). Read the file afterwards using fread() upto 8
characters only.
#include<stdio.h>
4
#include<stdlib.h>
int main() {
printf("Program Run By Rejina Bhattarai.\n");
char returnedstr[10];
FILE *fp;
fp = fopen("reju's_code.txt","w+");
if(fp == NULL){
printf("ERRORRR");
exit(1);
}
char str[] = "I Love C Programming";
fwrite(str,sizeof(char),sizeof(str)-1,fp);
rewind(fp);
fread(returnedstr,sizeof(char),9,fp);
returnedstr[9]='\0';
fclose(fp);
return 0;
}
5
5. Write a C program to store Name, Address, Age and Salary of 10 employees in a
data file STAFF.TXT.
#include<stdio.h>
#include<stdlib.h>
struct staff{
char Name[40];
char Address[40];
int Age;
int Salary;
};
int main() {
int i;
printf("Program Run By Rejina Bhattarai.\n");
for(i=0;i<10;i++){
printf("Staff [%d]\n",i+1);
printf("Name: ");
scanf("%s", s[i].Name);
6
printf("Address: ");
scanf("%s", s[i].Address);
printf("Age: ");
scanf("%d", &s[i].Age);
printf("Salary: ");
scanf("%d", &s[i].Salary);
}
FILE *fp;
fp = fopen("STAFF.txt", "w");
if(fp == NULL){
printf("ERROR");
exit(1);
}
7
8
6. Write a C program to display the records whose salary is greater than 15000 and
who is younger than 25 from the data file STAFF.TXT created in Q5. Display the
records in proper
format.
#include <stdio.h>
#include <stdlib.h>
struct Staff {
char name[50];
char address[100];
int age;
float salary;
};
int main() {
printf("Run by REJINA BHATTARAI\n");
FILE *fp;
struct Staff s;
fp = fopen("STAFF.txt", "r");
printf("Employees with salary > 15000 and age < 25:\n");
while (fscanf(fp, "%s %s %d %f", s.name, s.address, &s.age, &s.salary) != EOF) {
if (s.salary > 1500 && s.age < 25) {
printf("Name: %s, Address: %s, Age: %d, Salary: %.2f\n", s.name, s.address, s.age,
s.salary);
}
}
fclose(fp);
return 0;
}
9
QSN 7) Write a C program to add two new records of employees in STAFF.TXT
created in Q5.
#include<stdio.h>
#include<stdlib.h>
struct staff{
char Name[40];
char Address[40];
int Age;
int Salary;
};
int main() {
int i;
printf("Program Run By Rejina Bhattarai.\n");
for(i=0;i<2;i++){
printf("Staff [%d]\n",i+1);
printf("Name: ");
10
scanf("%s", s[i].Name);
printf("Address: ");
scanf("%s", s[i].Address);
printf("Age: ");
scanf("%d", &s[i].Age);
printf("Salary: ");
scanf("%d", &s[i].Salary);
}
FILE *fp;
fp = fopen("STAFF.txt", "a");
if(fp == NULL){
printf("ERROR");
exit(1);
}
11
8. Write a C program to read the STAFF.TXT file created in Q5 and save the
records of employees with Address “kathmandu” in a new KATHMANDU.TXT
file.
#include <stdio.h>
#include <string.h>
struct Employee {
char name[50];
char address[100];
int age;
int salary;
};
int main() {
12
if (staffFile == NULL) {
printf("Error opening STAFF.TXT\n");
return 1;
}
kathmanduFile = fopen("KATHMANDU.TXT", "w");
if (kathmanduFile == NULL) {
printf("Error opening KATHMANDU.TXT\n");
fclose(staffFile);
return 1;
13
9. Write a C program to display the records of the KATHMANDU.TXT file
created in Q8 and rename it with NEWKATHMANDU.TXT filename.
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
char address[100];
int age;
int salary;
};
int main() {
FILE *file;
struct Employee emp;
fclose(file);
rename("KATHMANDU.TXT", "NEWKATHMANDU.TXT");
return 0;
}
14
10) Write a C program to delete the file NEWKATHMANDU.TXT from Q9.
#include <stdio.h>
#include <stdlib.h>
int main() {
if (remove("NEWKATHMANDU.TXT") == 0) {
printf("File deleted successfully.\n");
}
else{
printf("Error deleting file.\n");
}
return 0;
}
15
16