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

Labsheet#4

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

Labsheet#4

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

Labsheet#4

Objective#1:
1.1 Program to write data into a text file and read and display the data.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *f;
char c;
clrscr();
f=fopen("data.txt","w");
if(f==NULL)
{
printf("File cannot be opened\n");
exit(1);
}
printf("Enter data to store in the file:\n");
while((c=getchar())!=’\n’)
{
fputc(c,f);
}
fclose(f);
f=fopen("data.txt","r");
if(f==NULL)
{
printf("File cannot be opened\n");
exit(1);
}
printf("\n Contents read from file:\n");
while((c=fgetc(f))!=EOF)
{
putchar(c);
}
fclose(f);
getch();
}
*Output:
Enter data to store in the file:
How are you today?

Contents read from file:


How are you today?

1.1. Note the output and modify the above program to open in append mode. Add the data to the file and
display the data before and after appending:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main() {
FILE *f;
char c;
clrscr();
f = fopen("data.txt", "r");
if (f == NULL) {
printf("File cannot be opened\n");
exit(1);
}

printf("Contents of the file before appending:\n");


while ((c = fgetc(f)) != EOF) {
putchar(c);
}
fclose(f);

f = fopen("data.txt", "a");
if (f == NULL) {
printf("File cannot be opened\n");
exit(1);
}

printf("\nEnter data to append to the file:\n");


while ((c = getchar()) != ’\n’) {
fputc(c, f);
}
fclose(f);

f = fopen("data.txt", "r");
if (f == NULL) {
printf("File cannot be opened\n");
exit(1);
}

printf("\nContents of the file after appending:\n");


while ((c = fgetc(f)) != EOF) {
putchar(c);
}
fclose(f);
getch();
}
*Output:
Contents of the file before appending:
How are you today?
Enter data to append to the file:
Good Morning

Contents of the file after appending:


How are you today?Good Morning

Objective#2

#include<stdio.h>
#include<string.h>

void main() {
FILE *p;
char text[3][10];
int i;
p = fopen("ram.txt", "w");
if (p == NULL) {
printf("File cannot be opened\n");
return;
}

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


scanf("%s", text[i]);
fputs(text[i], p);
fprintf(p, "\n");
}
fclose(p);

p = fopen("ram.txt", "r");
if (p == NULL) {
printf("File cannot be opened\n");
return;
}

printf("\nContents read from file:\n");

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


fgets(text[i], sizeof(text[i]), p);
printf("%s", text[i]);
}
fclose(p);
}
*Output:
Happy Birthday Boy

Contents read from file:


Happy
Birthday
Boy

Objective#3

3.1: Writing integers to a file using putw() and reading them back using getw() function.
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int num, i;
fp=fopen("number.dat","wb");
if(fp==NULL)
{
printf("Error opening file!\n");
return 1;
}
printf("Enter 5 integers:\n");
for(i=0;i<5;i++)
{
scanf("%d",&num);
fwrite(&num, sizeof(int), 1, fp);
}

fclose(fp);
fp=fopen("number.dat","rb");
if(fp==NULL)
{
printf("Error opening file!\n");
return 1;
}
printf("\nContents read from file:\n");
while(fread(&num, sizeof(int), 1, fp) == 1)
{
printf("%d\n",num);
}
fclose(fp);
return 0;
}

*Output:
Enter 5 integers:
12345

Contents read from file:


1
2
3
4
5

Objective#4
4.1) Write the formatted data in file using fprintf() function.

(a) Without using structure;

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char names[5][50];
int ids[5];
int i;
fp=fopen("employees.txt","w");
if(fp==NULL)
{
printf("Error opening file!\n");
return 1;
}
printf("Enter details of 5 employees:\n");
for(i=0;i<5;i++)
{
printf("Employee %d\n",i+1);
printf("Name:");
scanf("%s",names[i]);
printf("Employee ID:");
scanf("%d",&ids[i]);
fprintf(fp,"%s\t%d\n",names[i],ids[i]);
}
fclose(fp);
printf("Employee details written to file successfully:\n");
return 0;
}
*Output:
Enter details of 5 employees:
Employee 1
Name:Rupa
Employee ID:101
Employee 2
Name:Shyam
Employee ID:203
Employee 3
Name:Rita
Employee ID:326
Employee 4
Name:Shrijan
Employee ID:432
Employee 5
Name:Sita
Employee ID:221
Employee details written to file successfully:

(b) By using structure:


#include<stdio.h>
#include<conio.h>
struct employee
{
char name[20];
int employee_id;
};

int main()
{
FILE *fp;
struct employee emp[5];
int i;
clrscr();
fp=fopen("employee.txt","w");
if(fp==NULL)
{
printf("Error opening file!\n");
return 1;
}
printf("Enter details of 5 employees:\n");
for(i=0;i<5;i++)
{
printf("Employee %d\n",i+1);
printf("Name:");
scanf("%10s",emp[i].name);
printf("Employee ID:");
scanf("%d",&emp[i].employee_id);
fprintf(fp,"10%s\t%d\n",emp[i].name,emp[i].employee_id);
}
fclose(fp);
printf("Employee details written to file successfully:\n");
return 0;
}
*Output:
Enter details of 5 employees:
Employee 1
Name:Jhon
Employee ID:222
Employee 2
Name:Samip
Employee ID:333
Employee 3
Name:Jenisha
Employee ID:666
Employee 4
Name:Mina
Employee ID:444
Employee 5
Name:Manish
Employee ID:555
Employee details written to file successfully:

4.2 Read the formatted data in a file using fscanf() function.


#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char emp_name[50];
int emp_id;
clrscr();
fp=fopen("employees.txt","r");
if(fp==NULL)
{
printf("Error opening file!\n");
return 1;
}
while((fscanf(fp,"%10s%d",emp_name,&emp_id))!=EOF)
{
printf("Name:%s\tEmployee ID:%d\n",emp_name,emp_id);
}
fclose(fp);
return 0;
}
*Output:
Name:Rupa Employee ID:101
Name:Shyam Employee ID:203
Name:Rita Employee ID:326
Name:Shrijan Employee ID:432
Name:Sita Employee ID:221
4.3 Write and read details of student to/form the file
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int rollno;
float marks;
};

int main()
{
FILE *fp;
clrscr();
struct student students[5];
int i;
printf("Enter the details of 5 students:\n");
for(i=0;i<5;i++)
{
printf("Student %d:\n",i+1);
printf("Name:");
scanf("%19s",students[i].name);
printf("Roll number:");
scanf("%d",&students[i].rollno);
printf("Marks:");
scanf("%f",&students[i].marks);
printf("\n");
}
fp=fopen("student_data.txt","w");
if(fp==NULL)
{
printf("Error opening file for writing!\n");
return 1;
}
for(i=0;i<5;i++)
{
fprintf(fp,"%s %d %.2f\n",students[i].name,students[i].rollno,students[i].marks);
}
fclose(fp);
fp=fopen("student_data.txt","r");
if(fp==NULL)
{
printf("Error opening file for reading!\n");
return 1;
}

while(fscanf(fp,"%19s %d %f",students[i].name,&students[i].rollno,&students[i].marks) != EOF)


{
printf("Name:%s Roll:%d Marks:%f\n",students[i].name,students[i].rollno,students[i].marks);

}
fclose(fp);
return 0;
}
*Output:
Name:Ram
Roll number:1
Marks:85

Student 2:
Name:Shyam
Roll number:2
Marks:87

Student 3:
Name:Hari
Roll number:3
Marks:90

Student 4:
Name:Gita
Roll number:4
Marks:86

Student 5:
Name:Mina
Roll number:5
Marks:65

Name:Ram Roll:1 Marks:85.000000


Name:Shyam Roll:2 Marks:87.000000
Name:Hari Roll:3 Marks:90.000000
Name:Gita Roll:4 Marks:86.000000
Name:Mina Roll:5 Marks:65.000000

Objective#5
5.1 Use of fread() and fwrite() functions:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Record
{
char name[30];
int age;
float salary;
}rec;

int main()
{
FILE *fp;
clrscr();
fp=fopen("record.dat","w");
if(fp==NULL)
{
printf("Error opening file for writing!\n");
return 1;
}
printf("Enter record details:\n");
printf("Name:");
scanf("%29s",rec.name);
printf("Age:");
scanf("%d",&rec.age);
printf("Salary:");
scanf("%f",&rec.salary);
fwrite(&rec,sizeof(struct Record),1,fp);
fclose(fp);
fp=fopen("record.dat","r");
fread(&rec,sizeof(struct Record),1,fp);
printf("\nRecord read from file:\n");
printf("Name:%s\n",rec.name);
printf("Age:%d\n",rec.age);
printf("Salary:%.2f\n",rec.salary);
fclose(fp);
return 0;
}

*Output:
Enter record details:
Name:Ram
Age:20
Salary:20000

Record read from file:


Name:Ram
Age:20
Salary:20000.00

5.1: WAP to enter name, roll no and marks of 5 students and store them in file in the sequence of
decreasing marks using fwrite() function. Read and display the same from using the fread() function.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct student
{
char name[20];
int rollno;
float marks;
};

int main()
{
FILE *fp;
struct student students[5];
int i, j;
printf("Enter the details of 5 students:\n");
for(i=0; i<5; i++)
{
printf("Student %d:\n", i+1);
printf("Name:");
scanf("%19s", students[i].name);
printf("Roll number:");
scanf("%d", &students[i].rollno);
printf("Marks:");
scanf("%f", &students[i].marks);
printf("\n");
}

for(i=0; i<4; i++)


{
for(j=i+1; j<5; j++)
{
if(students[i].marks < students[j].marks)
{
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}

fp=fopen("student_data.dat","w");
if(fp==NULL)
{
printf("Error opening file for writing!\n");
return 1;
}
for(i=0; i<5; i++)
{
fwrite(&students[i], sizeof(struct student), 1, fp);
}
fclose(fp);

fp=fopen("student_data.dat","r");
if(fp==NULL)
{
printf("Error opening file for reading!\n");
return 1;
}
printf("\nStudents in decreasing order of marks:\n");
for(i=0; i<5; i++)
{
fread(&students[i], sizeof(struct student), 1, fp);
printf("Name:%s Roll:%d Marks:%.2f\n", students[i].name, students[i].rollno, students[i].marks);
}
fclose(fp);
return 0;
}
*Output:
Enter the details of 5 students:
Student 1:
Name:Ram
Roll number:32
Marks:85.6

Student 2:
Name:Rita
Roll number:33
Marks:81.5

Student 3:
Name:Aashish
Roll number:34
Marks:71.6

Student 4:
Name:Bipin
Roll number:35
Marks:88.4

Student 5:
Name:Mansoon
Roll number:36
Marks:75.3

Students in decreasing order of marks:


Name:Bipin Roll:35 Marks:88.40
Name:Ram Roll:32 Marks:85.60
Name:Rita Roll:33 Marks:81.50
Name:Mansoon Roll:36 Marks:75.30
Name:Aashish Roll:34 Marks:71.60

Conclusion:
The data file in C programming allow us to perform various operations, We can create a new file using
the fopen() function with the "w" or "wb" mode, write data to a file using functions like fprintf(), fputs(), and
fwrite(), and read data from a file using functions like fscanf(), fgets(), and fread(). Additionally, we can
append data to an existing file using the fopen() function with the "a" or "ab" mode, update an existing file
by reading and writing data to it using functions like fread() and fwrite(), and close a file after we’re done
using it to free up system resources using the fclose() function. When working with data files, it’s essential
to check for errors when opening and closing files, handle errors when reading or writing data, close files
when we’re done using them, and use the correct mode when opening a file (e.g., "r" for reading, "w" for
writing, "a" for appending). By following these best practices and using the I/O handling functions
correctly, we can effectively work with data files in C programming.

You might also like