Q.25 What are different file opening modes in ‘C’. Suppose a file contains student’s records with each record containing name and age of student. Write a ‘C’ program to read these records and display them in sorted order by name.
Ans. The different file opening modes in ‘C’ are.
(1) “r” : Open an existing file for reading only.
(2) “w” : Open a new file for writing only. If the file with the specified file-name currently exists, it will be destroyed and a new file will be created in its place.
(3) “a” : Open an existing file for appending (i.e. for adding new in formation at the end of the file). A new file will be created if the file with specified file-name does not exist.
(4) “r +” : Open an existing file for both reading and writing.
(5) “w +” : Open a new file for both reading and writing. If the file with the specified file-name currently exists, it will be destroyed and a new file will be created in its place.
(6) “a +” : Open an existing file for both reading and appending. A new file will be created if the file with the specified file-name does not exist.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
/*number of records */
const long records = 5;
/*file name*/
char fname[20] = "student_info.txt";
/* function prototypes */
void input_record(void);
void write_record_to_file(void);
void read_and_display_record_from_file(void);
/* structure of a person */
struct student{
char Name[30];
int age;
};
/* global variable declarations */
struct student stu[records], p1, p2;
FILE *fp;
void main()
{
long i;
for( i = 0; i < records; i++)
{
input_record();
write_record_to_file();
}
read_and_display_record_from_file();
}
void input_record()
{
clrscr();
printf("\n Enter NAME :\n");
scanf("%s",&p1.Name);
printf("\n Enter AGE :\n");
scanf("%d",&p1.age);
printf("\nPress any key to continue.");
getch();
}
void write_record_to_file(void)
{
if((fp = fopen(fname,"a"))!= NULL)
{
fprintf(fp,"%s\n",p1.Name);
fprintf(fp,"%d\n",p1.age);
fclose(fp);
}
else
{
printf("\nError opening a file");
printf("\nPress any key to exit");
getch();
exit(1);
}
}
void read_and_display_record_from_file(void)
{
if((fp = fopen(fname,"r"))!=NULL)
{
long i, j;
int cmp;
for(i = 0; i < records; i++)
{
fscanf( fp, "%s", &stu[i].Name);
fscanf( fp,"%d", &stu[i].age);
}
fclose(fp);
//Sorting array using Bubble Sort
for( i = 0; i < records; i++)
{
for( j = 0; j < records; j++)
{
cmp = strcmp(stu[j].Name, stu[j + 1].Name);
if(cmp > 0)
{
p2 = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = p2;
}
}
}
//Output
printf("\nList of students sorted by name :\n");
for( i = 0; i < records; i++)
{
printf("\nName :");
printf("%s",stu[i].Name);
printf("\nAge :");
printf("%d", stu[i].age);
}
printf("\nPress any key to continue.");
getch();
}
else
{
printf("\nError opening a file");
printf("\nPress any key to exit");
getch();
exit(1);
}
}
Ans. The different file opening modes in ‘C’ are.
(1) “r” : Open an existing file for reading only.
(2) “w” : Open a new file for writing only. If the file with the specified file-name currently exists, it will be destroyed and a new file will be created in its place.
(3) “a” : Open an existing file for appending (i.e. for adding new in formation at the end of the file). A new file will be created if the file with specified file-name does not exist.
(4) “r +” : Open an existing file for both reading and writing.
(5) “w +” : Open a new file for both reading and writing. If the file with the specified file-name currently exists, it will be destroyed and a new file will be created in its place.
(6) “a +” : Open an existing file for both reading and appending. A new file will be created if the file with the specified file-name does not exist.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
/*number of records */
const long records = 5;
/*file name*/
char fname[20] = "student_info.txt";
/* function prototypes */
void input_record(void);
void write_record_to_file(void);
void read_and_display_record_from_file(void);
/* structure of a person */
struct student{
char Name[30];
int age;
};
/* global variable declarations */
struct student stu[records], p1, p2;
FILE *fp;
void main()
{
long i;
for( i = 0; i < records; i++)
{
input_record();
write_record_to_file();
}
read_and_display_record_from_file();
}
void input_record()
{
clrscr();
printf("\n Enter NAME :\n");
scanf("%s",&p1.Name);
printf("\n Enter AGE :\n");
scanf("%d",&p1.age);
printf("\nPress any key to continue.");
getch();
}
void write_record_to_file(void)
{
if((fp = fopen(fname,"a"))!= NULL)
{
fprintf(fp,"%s\n",p1.Name);
fprintf(fp,"%d\n",p1.age);
fclose(fp);
}
else
{
printf("\nError opening a file");
printf("\nPress any key to exit");
getch();
exit(1);
}
}
void read_and_display_record_from_file(void)
{
if((fp = fopen(fname,"r"))!=NULL)
{
long i, j;
int cmp;
for(i = 0; i < records; i++)
{
fscanf( fp, "%s", &stu[i].Name);
fscanf( fp,"%d", &stu[i].age);
}
fclose(fp);
//Sorting array using Bubble Sort
for( i = 0; i < records; i++)
{
for( j = 0; j < records; j++)
{
cmp = strcmp(stu[j].Name, stu[j + 1].Name);
if(cmp > 0)
{
p2 = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = p2;
}
}
}
//Output
printf("\nList of students sorted by name :\n");
for( i = 0; i < records; i++)
{
printf("\nName :");
printf("%s",stu[i].Name);
printf("\nAge :");
printf("%d", stu[i].age);
}
printf("\nPress any key to continue.");
getch();
}
else
{
printf("\nError opening a file");
printf("\nPress any key to exit");
getch();
exit(1);
}
}