Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, January 9, 2023

Append Read Mode in C Programming

 Append Read Mode in C Programming

 

Append Read Mode in C Programming


Introduction


File handling is an essential part of programming, and C programming language provides various file handling functions and modes to read, write, append, and modify files. In this article, we will discuss the Append Read Mode in C Programming, its syntax, and provide examples of its usage.


What is Append Read Mode in C Programming?


The Append Read Mode is a file mode in C programming that allows users to open an existing file for both reading and appending data at the end of the file. The data is written to the end of the file, and the existing data is not overwritten.



Syntax of opening a file in Append Read Mode


The following syntax is used to open a file in Append Read Mode in C programming language:

FILE *fp; fp = fopen("filename", "a+");

In the above syntax, "filename" is the name of the file that needs to be opened in Append Read Mode. If the file does not exist, a new file with the specified name is created. If the file already exists, the file pointer is positioned at the end of the file, and the data is appended to the end of the file.



Examples of using Append Read Mode in C Programming


Appending data to an existing file using Append Read Mode


The following example demonstrates how to append data to an existing file using Append Read Mode:

#include <stdio.h> 
int main() 
FILE *fp; 
fp = fopen("example.txt", "a+"); 
fprintf(fp, "This is an example string."); 
fclose(fp); 
return 0; 
}

In the above example, we have opened a file called "example.txt" in Append Read Mode and appended the string "This is an example string." to the end of the file using the fprintf() function.


Reading data from a file using Append Read Mode


The following example demonstrates how to read data from a file using Append Read Mode:

#include <stdio.h> 
int main() 
{ FILE *fp; 
fp = fopen("example.txt", "a+"); 
char str[100]; 
fgets(str, 100, fp); 
printf("%s", str); 
fclose(fp); 
return 0; 
}


Advantages and Disadvantages of Append Read Mode in C programming


In C programming, the Append Read Mode is a file access mode that allows data to be appended to the end of an existing file, while also allowing read operations on the file. Here are some advantages and disadvantages of using Append Read Mode in C programming:

Advantages:
  1. Data can be easily added to an existing file without overwriting or deleting any existing data.
  2. It allows multiple processes or threads to access the file concurrently and safely, without the risk of overwriting data.
  3. It can be useful for creating log files, where new data is continuously added to the end of the file.

Disadvantages:
  1. It can make the file size unnecessarily large and difficult to manage over time, especially if the file is not periodically trimmed or cleaned.
  2. 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.
  3. 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;

}




Conclusion:


In conclusion, Append Read Mode in C Programming is a useful feature in C programming that allows new data to be appended to an existing file while permitting read operations on the file. While this mode offers several advantages, it also has some limitations that programmers should be aware of. By understanding the potential benefits and drawbacks of Append Read Mode, developers can make informed decisions about when and how to use this feature in their file management applications.


You can also Read:


FAQ: 


Q: How do I open a file in Append Read Mode in C programming? 
Ans: To open a file in Append Read Mode, use the "a+" mode specifier in the fopen() function.

Q: Can I use Append Read Mode with binary files? 
Ans: Yes, Append Read Mode can be used with both text and binary files.

Q: Is it safe to use Append Read Mode with multiple processes or threads? 
Ans: Yes, Append Read Mode is designed to allow safe concurrent access to files, but it is important to implement proper synchronization and error-handling techniques to avoid data corruption or file damage.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages