0% found this document useful (0 votes)
4 views

841_FileHandling

Uploaded by

punkcybe770
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

841_FileHandling

Uploaded by

punkcybe770
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

ST.

XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU, NEPAL
Phone: 01-5321365, 01-5344636
Email: [email protected]

LAB ASSIGNMENT NUMBER: 9

“C PROGRAMMING -
FILE HANDLING”

Submitted By Submitted To Signature


Name: Rejina Bhattarai Mrs. Jamuna Maharjan
Roll No: 023NEB841 Department of Computer
Class:12 Science,
Section:H St. Xavier’s College

Submission Date: 15 September, 2024


TABLE OF CONTENTS

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);
}

printf("Enter a character: ");


scanf("%c",&c);

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);
}

printf("Enter an integer: ");


scanf("%d",&c);

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

string afterwards using fgets().

#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);

printf("You entered %s", returnedStr);


fclose(fp);
return 0;
}

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';

printf("String read from file upto 8 characters: %s",returnedstr);

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");

struct staff s[10];

printf("Enter name,address,age and salary in sequence order;\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);
}

for(i = 0; i < 10; i++) {


fprintf(fp, "%s\t", s[i].Name);
fprintf(fp, "%s\t", s[i].Address);
fprintf(fp, “%d\t", s[i].Age);
fprintf(fp, "%d\n", s[i].Salary);
}
fclose(fp);
return 0;
}

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");

struct staff s[2];

printf("Enter name,address,age and salary in sequence order;\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);
}

for(i = 0; i < 2; i++) {


fprintf(fp, "%s\t", s[i].Name);
fprintf(fp, "%s\t", s[i].Address);
fprintf(fp, "%d\t", s[i].Age);
fprintf(fp, "%d\n", s[i].Salary);
}
fclose(fp);
return 0;
}

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() {

printf("Run by Rejina Bhattarai\n");


FILE *staffFile, *kathmanduFile;
struct Employee emp;

staffFile = fopen("STAFF.TXT", "r");

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;

while (fscanf(staffFile, "%s %s %d %d", emp.name, emp.address, &emp.age, &emp.salary) != EOF)


{
// Use case-insensitive comparison for "kathmandu"
if (strcasecmp(emp.address, "kathmandu") == 0) {
fprintf(kathmanduFile, "%s %s %d %d\n", emp.name, emp.address, emp.age, emp.salary);
}
}
fclose(staffFile);
fclose(kathmanduFile);
printf("Records with address 'kathmandu' saved in KATHMANDU.TXT\n");
return 0;
}

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() {

printf("Run by Rejina Bhattarai\n");

FILE *file;
struct Employee emp;

file = fopen("KATHMANDU.TXT", "r");


printf("Records from KATHMANDU.TXT:\n");

while (fscanf(file, "%s %s %d %f", emp.name, emp.address, &emp.age, &emp.salary) != EOF) {


printf("Name: %s, Address: %s, Age: %d, Salary: %d\n", emp.name, emp.address, emp.age,
emp.salary);
}

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() {

printf("Run by Rejina Bhattarai\n");

if (remove("NEWKATHMANDU.TXT") == 0) {
printf("File deleted successfully.\n");
}
else{
printf("Error deleting file.\n");
}
return 0;
}

15
16

You might also like