Labsheet#4
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?
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);
}
f = fopen("data.txt", "a");
if (f == NULL) {
printf("File cannot be opened\n");
exit(1);
}
f = fopen("data.txt", "r");
if (f == NULL) {
printf("File cannot be opened\n");
exit(1);
}
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;
}
p = fopen("ram.txt", "r");
if (p == NULL) {
printf("File cannot be opened\n");
return;
}
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
Objective#4
4.1) Write the formatted data in file using fprintf() function.
#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:
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:
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;
}
}
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
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
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");
}
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
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.