-->

Lab works-09(data files)

 Questions:

q1) Write a program to enter name,roll_no, and marks of 10 students and store them in the file.

q2)Write a program to store empid,name and salary of 10 employees in a datafile and print the records from the file.

q3)Write a program using C language that reads successive records from new data file and display each record on the screen in an appropriate format.

q4)Write a program to store std_id,name and marks of 'n' students in a data file.Display the records in appropriate format reading from the file.

q5)Write a program to enter name,roll_no and marks of 10 students and store them in the file.Read and display the record from the file.

-------------------------------------------------------------------------------------------------

solution:-

q1) Write a program to enter name,roll_no, and marks of 10 students and store them in the file.

ans:-

code:

//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30];                                                    //identifier declaration
int roll,count=1;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
do
{

printf("enter record=%d",count);
printf("\nenter roll\n");   //data input
scanf("%d",&roll);

printf("enter  name\n");           // getting inputs

scanf("%s",name);
printf("enter percentage\n");
scanf("%f",&per);

fprintf(k,"%d %s %f\n",roll,name,per); //writing data to data file
printf("-------------------------------\n");//prints message
count++;                           
}while(count<=10);        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                       
getch();
return 0;
}

q2)Write a program to store empid,name and salary of 10 employees in a datafile and print the records from the file.

Ans:-

code:

//to store empid,name and salary of 10 employees
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char ename[30];                                                    //identifier declaration
int empid,count=1;                                                              //       "             "
float esalary;
k=fopen("emp.txt","w");//file opening
do
{

printf("enter record for=%d employee",count);
printf("\nenter empid\n");   //data input
scanf("%d",&empid);

printf("enter  ename\n");           // getting inputs
scanf("%s",ename);
printf("enter salary\n");
scanf("%f",&esalary);

fprintf(k,"%d %s %f\n",empid,ename,esalary); //writing data to data file
printf("-------------------------------\n");//prints message
count++;                           
}while(count<=10);        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file    

k=fopen("emp.txt","r");//file opening

while((fscanf(k,"%d %s %f\n",empid,ename,esalary))!=EOF)   
//reading process goes on until the file ends(End of File)
{
printf("empid=%d name=%s salary=%f\n",empid,ename,esalary);   //printing on screen
}

printf("\n reading process completed successfully\n");                   
getch();
return 0;
}

q3)Write a program using C language that reads successive records from new data file and display each record on the screen in an appropriate format.
ans:
code:-
//to read empid,name and salary of  employees from data file
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char ename[30];                                                    //identifier declaration
int empid,count=1;                                                              //       "             "
float esalary;
k=fopen("emp.txt","r");//file opening
printf("read records are:\n");
while((fscanf(k,"%d %s %f\n",empid,ename,esalary))!=EOF)
//reading process goes on until the file ends(End of File)
{
printf("empid=%d name=%s salary=%f\n",empid,ename,esalary);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file
getch();
return 0;
}

q4)Write a program to store std_id,name and marks of 'n' students in a data file.Display the records in appropriate format reading from the file.

ans:-

code:-

//to store std_id,name and marks(5 subjects) of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char sname[30],choice;                                                    //identifier declaration
int sid;                                                              //       "             "
int eng,acc,eco,bstd,csc;
k=fopen("sdetail.txt","w");//file opening
printf("we are going to write /store data\n");
do
{
printf("\nenter sid\n");   //data input
scanf("%d",&sid);

printf("enter  name\n");           // getting inputs
scanf("%s",sname);
printf("enter marks of english\n");
scanf("%d",&eng);

printf("enter marks of account\n");
scanf("%d",&acc);

printf("enter marks of eco.\n");
scanf("%d",&eco);

printf("enter marks of bstd\n");
scanf("%d",&bstd);

printf("enter marks of csc\n");
scanf("%d",&csc);

fprintf(k,"%d %s %d %d %d %d %d\n",sid,sname,eng,acc,eco,bstd,csc); //writing data to data file
printf("want to continue (y/n)\n");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------------------------------\n");
printf("read records are:\n");
k=fopen("sdetail.txt","r");//file opening

while((fscanf(k,"%d %s %d %d %d %d %d",&sid,sname,&eng,&acc,&eco,&bstd,&csc))!=EOF)
//inputs goes until the file ends(End of File)
{
printf("sid=%d sname=%s ,eng=%d acc=%d,eco=%d,bstd=%d,csc=%d\n",sid,sname,eng,acc,eco,bstd,csc);  //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file

getch();
return 0;
}

q5)Write a program to enter name,roll_no and marks of 10 students and store them in the file.Read and display the record from the file.

ans:-

code:

//to store std_id,name and marks(5 subjects) of 10 students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char sname[30];                                                    //identifier declaration
int roll_no,count=1;                                                              //       "             "
int eng,acc,eco,bstd,csc;
k=fopen("sdetail.txt","w");//file opening
printf("we are going to write /store data\n");
do
{

printf("enter record for %d student\n",count);
printf("\nenter roll no.\n");   //data input
scanf("%d",&roll_no);

printf("enter  name\n");           // getting inputs
scanf("%s",sname);
printf("enter marks of english\n");
scanf("%d",&eng);

printf("enter marks of account\n");
scanf("%d",&acc);

printf("enter marks of eco.\n");
scanf("%d",&eco);

printf("enter marks of bstd\n");
scanf("%d",&bstd);

printf("enter marks of csc\n");
scanf("%d",&csc);

fprintf(k,"%d %s %d %d %d %d %d\n",roll_no,sname,eng,acc,eco,bstd,csc); //writing data to data file
count++;

printf("-----------------------------------\n");
}while(count<=10);      
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------------------------------\n");
printf("read records are:\n");
k=fopen("sdetail.txt","r");//file opening

while((fscanf(k,"%d %s %d %d %d %d %d",&roll_no,sname,&eng,&acc,&eco,&bstd,&csc))!=EOF)
//inputs goes until the file ends(End of File)
{
printf("roll no=%d sname=%s ,eng=%d acc=%d,eco=%d,bstd=%d,csc=%d\n",roll_no,sname,eng,acc,eco,bstd,csc);  //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file

getch();
return 0;
}


----------------------------------------------------------------------------------------------------------------------

case study:

Write a menu driven program which performs the following tasks.

1.Create a data file named "class12.data" and write record(roll_no,name,class and age) of student until 'n' is typed.

2.read all records from same file and display on the screen in appropriate format.

3.Display records of students whose age is more than 18.

4.Delete the particular record from the datafile on the basis of roll

5.Exit


No comments:

Post a Comment