Files

A file is a collection of information stored on a storage device with a particular file name.
Uses of a file:-
1) Stores the data in the form of a file and we can retrieve it whenever we require.
2) Using the data from the file in different programs.
Opening a file:- we can open a file by using   
fopen().
Syntax:- fopen(file name, opening mode);
Ex: - FILE *fp;
fp=fopen(“sssit.txt”,”w”); 
File opening modes:-
1)w :- Writing the data into a file.  If the file already exists its contents will be over written.
2)r:-  Reading data from a file.
3)a:-  Adds data into an existing file.(Appending)
closing a file:-      When the file processing is over, the file must be closed. We can close a file by using fclose( ).
syntax:- fclose(file pointer);
ex:- fclose(fp);
File functions:- 
1)fputc()
This function is used to write a character into a file.
syntax:-    fputc(character, file pointer);
ex:- fputc(ch,fp);
Write a program write a character to the file.
#include<stdio.h>
main()
{
FILE *fp;
char a;
clrscr();
fp=fopen("1.txt","w");
printf("Enter a character:");
scanf("%c",&a);
fputc(a,fp);
fclose(fp);
printf("Character copied to the File.");
getch();
}
Write a program add a character to the existing file.
#include<stdio.h>
main()
{
FILE *fp;
char a;
clrscr();
fp=fopen("1.txt","a");
printf("Enter a character:");
scanf("%c",&a);
fputc(a,fp);
fclose(fp);
printf("Character copied to the File.");
getch();
}

2)fgetc()
This function is used to read a character from a file.
syntax :-  getc(file pointer);
ex:- fgetc(fp);
Wap read a character from the file.
#include<stdio.h>
main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("1.txt","r");
if(fp==NULL)
{
printf("Unable to open the file.");
exit(0);
}
else
c=fgetc(fp);
printf("\nThe character is %c",c);
fclose(fp);
getch();
}
WAP write a line of data to the file.
#include<stdio.h>
main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("ss.txt","a");
printf("Enter  a line of Data:");
while(1)
{
scanf("%c",&c);
if(c!='\n')
fputc(c,fp);
else
break;
}
printf("Data Copied to the file");
getch();
}
WAP read line of data to the file.
#include<stdio.h>
main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("ss.txt","r");
printf("The line of Data is :");
while(1)
{
c=fgetc(fp);
if(c!=EOF)
printf(“%c”,c);
else
break;
}
getch();
}
3)fprintf():-  This function writes formatted data into a file.
Syntax:-           fprintf(file pointer, “formatted string”, list of variables);
Ex:-      fp=fopen(“student.dat”,”w”);
   fprintf (fp , “%d %s %d”,sno ,name, marks);
WAP write employee information(empno,name,sal) to the file.
#include<stdio.h>
main()
{
int eno,sal;
char nm[20];
FILE *fp;
fp=fopen("sam.txt","a");
printf("Enter eno,name,sal");
scanf("%d%s%d",&eno,&nm,&sal);
fprintf(fp,"\n%d\t%s\t%d",eno,nm,sal);
printf("File Written.");
getch();
}
WAP write employee information (empno,name,sal,bonus) to the file.
#include<stdio.h>
main()
{
int sal,bo,ns;
char nm[20];
FILE *fp;
fp=fopen("sssit.txt","a");
printf("Enter name,sal");
scanf("%s%d",&nm,&sal);
if(sal>10000)
{
bo=1000;
ns=sal+bo;
}
else
{
bo=500;
ns=sal+bo;
}
fprintf(fp,"\n%s\t%d\t%d\t%d",nm,sal,bo,ns);
printf("File Written.");
getch();
}
4)fscanf():-  This function reads formatted data from a file.
Syntax:-           fscanf(file pointer, “formatted string”, list of variables)
Ex:- fp=fopen(“student.dat”,”r”);
            fscanf(fp , “%d %s %d”,&sno ,name,&marks);
Wap to read the employee information with their net salaries from the file.
#include<stdio.h>
main()
{
int sal,bo,ns;
char nm[20];
FILE *fp;
clrscr();
fp=fopen("sssit.txt","r");
if(fp==NULL)
printf("Unable to open the file");
else
printf("\nEmployee Information\n");
while(fscanf(fp,"%s%d%d",&nm,&sal,&bo)!=EOF)
{
ns=sal+bo;
printf("\n%s\t%d\t%d\t%d",nm,sal,bo,ns);
}
getch();
}
WAP read employee information and printing.
struct emp
{
            int eno;
            char ename[20];
            float esal;
}erec;
#include<stdio.h>
main()
{
            int temp,found,ch;
            float sal;
            FILE *fp,*fp1;
            clrscr();
            do
            {
clrscr();
gotoxy(30,1); printf("1. Add New Record");
gotoxy(30,2); printf("2. Enquiry on a Record");
gotoxy(30,3); printf("3. Modify a Record");
gotoxy(30,4); printf("4. Delete a Record");
gotoxy(30,5); printf("5. List all Records");
gotoxy(30,6); printf("6. Exit");
gotoxy(30,7); printf("Enter Your Choice :");
            scanf("%d", &ch);
                        switch(ch)
                        {
                        case 1:
            fp=fopen("emp.dat", "a+");
            printf("\n Enter Employee Number :");
            scanf("%d", &erec.eno);
            printf("\n Enter Employee Name :");
            fflush(stdin);
            gets(erec.ename);
            printf("\n Enter Employee Salary :");
            scanf("%f", &sal);
            erec.esal=sal;
            fwrite(&erec, sizeof(erec),1,fp);
            printf("\n Record Added");
            fclose(fp);
            getch();
            break;
            case 2:
            printf("\n Enter Employee Number to Display :");
            scanf("%d", &temp);
            fp=fopen("emp.dat", "r+");
if(!fp)
{
printf("\n Unable to Open Source File");
getch();
exit(0);
}
found=0;
fread(&erec, sizeof(erec),1,fp);
while(!feof(fp))
{
if(erec.eno==temp)
{
found=1;
printf("\n Employee Name :%s", erec.ename);
printf("\n Employee Salary :%.2f", erec.esal);
break;
}
else
fread(&erec,sizeof(erec),1,fp);
}
if(found==0)
printf("\n No such Employee");
getch();
break;
case 3:
printf("\n Enter Employee Number to Modify :");
scanf("%d", &temp);
fp=fopen("emp.dat","r+");
fp1=fopen("temp.dat", "a+");
if(!fp)
{
printf("\n Unable to Open Source File");
getch();
exit(0);
}
found=0;
fread(&erec, sizeof(erec),1,fp);
while(!feof(fp))
{
if(temp==erec.eno)
{
found=1;
printf("\n Employee Name     :%s", erec.ename);
printf("\n Employee Salary   :%.2f", erec.esal);
printf("\n Enter Employee Number :");
scanf("%d", &erec.eno);
printf("\n Enter Employee Name :");
fflush(stdin);
gets(erec.ename);
printf("\n Enter Employee Salary :");
scanf("%f", &sal);
erec.esal=sal;
fwrite(&erec,sizeof(erec),1,fp1);
printf("\n Record Modified");
}
else
fwrite(&erec,sizeof(erec),1,fp1);
fread(&erec,sizeof(erec),1,fp);
}
if(found==0)
printf("\n No such employee to modify");
fclose(fp);
fclose(fp1);
remove("emp.dat");
rename("temp.dat", "emp.dat");
break;
getch();
case 4:
printf("\n Enter Employee Number to Delete :");
scanf("%d", &temp);
fp=fopen("emp.dat","r+");
fp1=fopen("temp.dat", "a+");
if(!fp)
{
printf("\n Unable to Open Source File");
getch();
exit(0);
}
found=0;
fread(&erec, sizeof(erec),1,fp);
while(!feof(fp))
{
if(temp==erec.eno)
{
found=1;
printf("\n Employee Name     :%s", erec.ename);
printf("\n Employee Salary   :%.2f", erec.esal);
printf("\n Record Deleted");
}
else
fwrite(&erec,sizeof(erec),1,fp1);
fread(&erec,sizeof(erec),1,fp);
}
if(found==0)
printf("\n No such employee to delete");
fclose(fp);
fclose(fp1);
remove("emp.dat");
rename("temp.dat", "emp.dat");
getch();
break;
case 5:
fp=fopen("emp.dat", "r");
if(!fp)
{
printf("\n Unable to Open Source File");
getch();
exit(0);
}
fread(&erec, sizeof(erec),1,fp);
while(!feof(fp))
{
printf("%d\t%s\t%.2f\n", erec.eno,erec.ename,erec.esal);
fread(&erec,sizeof(erec),1,fp);
}
fclose(fp);
getch();
break;
case 6:
            exit(0);
default :
            printf("\n Invalid Chocie");
            getch();
            break;
            }
}
while(1);
getch();

}

No comments:

Post a Comment