C Lab 8
C Lab 8
#include <stdio.h>
int main()
{
char name[50],address[20];
int num;
char tel[20];
printf("Enter number of students: ");
scanf("%d", &num);
FILE *fptr;
fptr = (fopen("D:\\student1.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
}
for(int i=0; i <num;i++ )
{
printf("For Employee%d \n",i+1);
printf("Enter name:");
scanf("%s",&name);
printf("Enter address:");
scanf("%s",address);
printf("Enter telephone:");
scanf("%s",tel);
fprintf(fptr,"\n Name: %s \t\tAddress:%s\t\t Telephone:%s",name,address,tel);
}
fclose(fptr);
return 0;
}
Example 2: Write a program to create “student.txt” file to store the Roll_no. Name, Address,
faculty Date of birth (mm-dd-yy) records for 100 students.
#include<stdio.h>
int main()
{
FILE *fptr;
char Name[20], Address[20],Faculty[20];
int Month, Day,Year,Roll;
fptr=fopen("D:\\students.txt","w");
if(fptr==NULL)
{
printf("\n File cannot be created");
}
fprintf(fptr, " Roll number\tName\tAddress\tFaculty\tDate of birth- MonthDay Year");
for(int i=0;i<100;i++)
{
printf("For Student%d",i);
printf("\nRoll\n");
scanf("%d",&Roll);
printf("Name\n");
scanf("%s",&Name);
printf("Address\n");
scanf("%s",&Address);
printf("Faculty\n");
scanf("%s",&Faculty);
printf("Date of Birth in Month-Day-Year\n");
scanf("%d%d%d",&Month,&Day,&Year);
fprintf(fptr,"\n %d\t%s\t%s\t%s\t%d-%d-%d" , Roll,Name,Address,Faculty,Month,Day,Year);
}
fclose(fptr);
return 0;
}
3. Example: Write a program to read the name, author, and the price of 500 books in a library
from the file 'library.dat'. Now print the book name and price of those books whose price is
above Rs.300.
#include <stdio.h>
int main()
{
char BookName[20], Author[20];
float Price;
FILE *fptr;
fptr=fopen("D:\\library.dat","r");
if(fptr==NULL)
{
printf("Error!");
}
for(int i=0;i<2;++i)
{
printf("Enter Book Name");
scanf("%s",BookName);
printf("Enter Author Name");
scanf("%s",Author);
printf("Enter Book Price");
scanf("%f",&Price);
if(Price>300)
{
fprintf(fptr,"\n %s \t %f ", BookName,Price);
}
}
fclose(fptr);
return 0;
}
Example: Write a program to input a name , address, faculty, program and GPA (in maximum
4.0) of 500 students and store them in 'RESULT.DAT' data
file and display the records of those students whose faculty is 'Engineering" and GPA 3.5.
#include <stdio.h>
int main()
{
char name[50],address[20],faculty[20],program[20];
int num,i;
float GPA;
printf("Enter number of students: ");
scanf("%d", &num);
FILE *fptr;
fptr = (fopen("D:\\Result.txt", "w"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr,"\nName\t\tAddress\t\tFaculty\t\tProgram\t\tGPA\n");
for( i=0; i <num;i++ )
{
printf("For student %d \n",i+1);
printf("Enter name:");
scanf("%s",&name);
printf("Enter address:");
scanf("%s",&address);
printf("Enter faculty:");
scanf("%s",&faculty);
printf("Enter program");
scanf("%s", &program);
printf("Enter GPA");
scanf("%f",&GPA);
if(faculty=='engineering'||'Engineering' &&GPA==3.5)
{
fprintf(fptr,"%s\t\t%s\t\t%s\t\t%s\t\t%f\n",name,address,faculty,program,GPA);
}
}
fclose(fptr);
return 0;
}