Append Read Mode in C Programming
Introduction
What is Append Read Mode in C Programming?
Syntax of opening a file in Append Read Mode
Examples of using Append Read Mode in C Programming
Appending data to an existing file using Append Read Mode
Reading data from a file using Append Read Mode
Advantages and Disadvantages of Append Read Mode in C programming
- Data can be easily added to an existing file without overwriting or deleting any existing data.
- It allows multiple processes or threads to access the file concurrently and safely, without the risk of overwriting data.
- It can be useful for creating log files, where new data is continuously added to the end of the file.
- It can make the file size unnecessarily large and difficult to manage over time, especially if the file is not periodically trimmed or cleaned.
- It can lead to slower read operations, especially if the file is very large and the operating system has to search through a lot of data to find a specific section.
- It can increase the risk of data corruption or file damage if multiple processes or threads try to access the same section of the file simultaneously.
Following are the different examples of Append Read Mode in C programming
Example
1:
A
sequential data file "result.dat" contains name, age, and
percentage. Write a C program to add a few new records in an existing data file and
display them on the screen.
#include<stdio.h>
#include<conio.h>
main()
{
char
name[20];
int
age;
float
per;
char
c='y';
FILE
*fp;
fp=fopen("d:\\result.dat","a+");
while(c=='y')
{
printf("enter
name\n");
scanf("%s",name);
printf("Enter
age\n");
scanf("%d",&age);
printf("Enter
percentae\n");
scanf("%f",&per);
fprintf(fp,"%s%d%f",name,age,per);
printf("Do
you need more records press y");
c=getche();
}
rewind(fp);
while(fscanf(fp,"%s\t%d\t%f\n",name,age,per)!=EOF)
{
printf("\n%s\t%d\t%f",name,age,per);
}
fclose(fp);
}
Example 2:
A data file "emp.txt" contains the name post and salary. Write a program to add a few new records in the same data
file and display them on the monitor in the appropriate format.
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *fp;
char name[20], post[30]
ch='y';
int salary;
fp = fopen("emp.txt",
"a");
while(c!='n')
{
printf("Enter name: ");
scanf("%s", name);
printf("Enter post: ");
scanf("%s", post);
printf("Enter salary: ");
scanf("%d", &salary);
fprintf(fp, "%s %s %d\n", name,
post, salary);
c=getche();
}
fclose(fp);
fp = fopen("emp.txt",
"r");
printf("Employee records:\n");
printf("----------------\n");
while (fscanf(fp, "%s %s %d", name,
post, &salary) != EOF)
{
printf("Name: %s\n", name);
printf("Post: %s\n", post);
printf("Salary: %d\n", salary);
printf("----------------\n");
}
fclose(fp);
return 0;
}
Example
3:
Write
a c program to add 10 new records in data file "emp.txt" which has
content name, post and salary and display all the records form the data file.
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
FILE
*fp;
char name[100];
char post[100];
int salary;
int i;
fp = fopen("emp.txt",
"a+");
for (i = 0; i < 10; i++)
{
printf("Enter
name: ");
scanf("%s",
name);
printf("Enter
post: ");
scanf("%s",
post);
printf("Enter
salary: ");
scanf("%d",
&salary);
fprintf(fp, "%s %s %d\n", name,
post, salary);
}
fclose(fp);
// Display records on the monitor
rewind(fp);
printf("Employee
records:\n");
printf("----------------\n");
while
(fscanf(fp, "%s %s %d", name, post, &salary) != EOF)
{
printf("Name:
%s\n", name);
printf("Post:
%s\n", post);
printf("Salary:
%d\n", salary);
printf("----------------\n");
}
fclose(fp);
return
0;
}
Example
4
A sequential data file teacher.dat has
contain name, department and salary. Write a c program to add 'n' number of
records in existing data file and display them on the screen in appropriate
format.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char name[100];
char department[100];
int salary;
int n;
int i;
fp =
fopen("teacher.dat", "a+");
printf("Enter the number of records to
add: ");
scanf("%d", &n);
// Add
new records to the file here
for (i
= 0; i < n; i++)
{
printf("Enter name: ");
scanf("%s", name);
printf("Enter department: ");
scanf("%s", department);
printf("Enter salary: ");
scanf("%d", &salary);
fprintf(fp, "%s %s %d\n", name,
department, salary);
}
fclose(fp);
// Display records on the screen
fp = fopen("teacher.dat",
"r");
printf("Teacher records:\n");
printf("----------------\n");
while
(fscanf(fp, "%s %s %d", name, department, &salary) != EOF)
{
printf("Name: %s\n", name);
printf("Department: %s\n",
department);
printf("Salary: %d\n", salary);
printf("----------------\n");
}
fclose(fp);
return 0;
}
No comments:
Post a Comment