0% found this document useful (0 votes)
1 views14 pages

C Programming - Files Handling

The document provides an overview of file handling in C programming, detailing various file operations such as creation, reading, writing, and closing files. It explains the use of functions like fopen, fclose, fprintf, and fscanf, along with examples of programs that demonstrate these operations. Additionally, it covers the creation of files for storing integers, copying file contents, and managing records using structures.

Uploaded by

Lalman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views14 pages

C Programming - Files Handling

The document provides an overview of file handling in C programming, detailing various file operations such as creation, reading, writing, and closing files. It explains the use of functions like fopen, fclose, fprintf, and fscanf, along with examples of programs that demonstrate these operations. Additionally, it covers the creation of files for storing integers, copying file contents, and managing records using structures.

Uploaded by

Lalman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Files Handling

Many application require that information be written to or read from Auxiliry


memeory device (Harddisk CD). Using the C program How to store the information on
the disk or how to aceess the information is called the FILE HANDLING.

Data organization
All data stored on the disk in a binary from this binary data is stored on the disk
varius form on operating system (OS) to another. It have to use any library function
written for the particular OS to be able to perform input/Output

Our Program C library Program O.S Disk

File Operation
There are different operations that can be carried out on a file

a) Creation a New File


b) Opening an Existing File
c) Reading from a file
d) Writing to a File
e) Moving to a specific location in a file(Seeking)
f) Closing a File

When working with a stream oriented data file, the first step is to establish a
buffer area where information is temporarily stored while being transferred between the
computer memory and the data file

FILE *fp;
Where FILE (upper case letter) is a special structure the buffer area and fp is
pointer variable that indicate the beginning of the buffer area.

I/O Function

fopen() Create a new file for use open an existing file for use.
fclose() Close a file which has been opened for use

File type/Mode

“r” Open an existing file for reading only


“w” Open a new file for writing only if a file with the specified file-name
currently existing, it will be destroy and a new file created into place
“a” Open an existing file for appending.
Opening a file
If you want to store in a file in the secondary memory, we must specify certain
things about the file
a) File name
b) Mode
c)
Syntax file *fp;
fp=fopen(“file_name”,”mode”);
Closing a file
The file must be closed as soon all operation on it have been completed
Syntex
fclose(fp);

1. Write a C program to check out file is found are not found.

void main()
{
FILE *fp;
char name[20];
clrscr();
printf(“\n Enter the file that you want to search”);
gets(name);
fp=fopen("name","w");
if(fp==NULL)
printf("\n File is not found ");
else
printf("\n File is available ");
fclose(fp);
getch();
}

GETC( ) and PUTC( ) function

The Fle I/O function are getc and putc.


Assume that a file is opened with mode “w” and file pointer fp and you write a
character to character into a particular file.

putc(c,fp);
Getc is used to read a character from that has been opened on read mode “r”

c=getc(fp)

2. Write a C program accept any name again write name in the particular file then display
on the screen from the file using getc() and putc()

void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("abc.txt","w");
printf("\n Enter any name ");
while((c=getchar())!='\n')
{
putc(c,fp);
}
fclose(fp);
fp=fopen("abc.txt","r");
while((c=getc(fp))!=EOF)
{
putchar(c);
}
fclose(fp);
getch();
}

The fprintf() and fscanf() function


The function fprintf and fscanf perform input and output opeation that are
identical to the familiar printf and scanf function, except that they work on file

Syntex fprintf(fp, “Control String”,List of variable);


Where fp is a file pointer associated with a file that has been open for writing. The
control string contains output specification for the items in the list, the list may include
variable constant and string
fprintf(fp, “%s%d”,name,age);
Here name is a char type array and age is int type variable

Syntex fscanf(fp, “Control String”,List of variable);


This statement is reading of the items in the list from the file specified by
according to the specification contained in the control string
fscanf(fp, “%s%d”,name,&age);
Here name is a char type array and age is int type variable

3. Write a C program accept any name again write name in the particular file then display
on the screen from the file using fprintf() and fscanf()
void main()
{
FILE *fp;
int i=0;
char name[20],str[20];
clrscr();
fp=fopen("abc.txt","w");
printf("\n Enter any name ");
while((name[i++]=getchar())!='\n');
fprintf(fp,"%s",name);
fclose(fp);
fp=fopen("abc.txt","r");
fscanf(fp,"%s",str);
printf("\n%s",str);
fclose(fp);
getch();
}
4. Write a C program find out the area of circle of given radius of circle using file handling

#define PI 3.14
void main()
{
FILE *fp;
int r;
float a;
fp=fopen("abc.txt","w");
clrscr();
printf("\n Enter the Radius =?");
scanf("%d",&r);
putw(r,fp);
fclose(fp);

fp=fopen("abc.txt","r");
r=getw(fp);
a=PI*r*r;
printf("\n Area of clrcle %f",a);
fclose(fp);
getch();
}

5. Create a file “number.txt” and strore list of 100 integers in it. Using the file
“number.dat”, write a C program which create two more to store even and odd integers
separately.

6. Write a Program to read 1 to 100 and write even number in a file(even.dat) and odd
number(odd.dat)

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fp1,*fp2;
int n,i,m;
clrscr();
fp=fopen("number.txt","w");
printf("\n Enter 10 number ");
for(i=0;i<10;i++)
{
printf("\n Enter %d number",i+1);
scanf("%d",&n);
putw(n,fp);
}
fclose(fp);

fp=fopen("number.txt","r");
printf("\n Odd Number :");
while((n=getw(fp))!=EOF)
{
printf(" %d",n);
}
fclose(fp);

fp=fopen("number.txt","r");
fp1=fopen("odd.txt","w");
fp2=fopen("even.txt","w");
while((n=getw(fp))!=EOF)
{
if(n%2==0)
putw(n,fp2);
else
putw(n,fp1);

}
fclose(fp);
fclose(fp1);
fclose(fp2);

fp=fopen("even.txt","r");
printf("\n Even Number :");
while((n=getw(fp))!=EOF)
{
printf(" %d",n);
}
fclose(fp);

fp=fopen("odd.txt","r");
printf("\n Odd Number :");
while((n=getw(fp))!=EOF)
{
printf(" %d",n);
}
fclose(fp);
getch();
}
7. Write a program in C that takes ten integers from a file and write square of these integer
into another file

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp, *fp1;
int n,s;
clrscr();
fp=fopen("abc.txt","w");
printf("\n Tyep 0 when finished ");
printf("\n Enter any number ");
scanf("%d",&n);
while(n!=0)
{
putw(n,fp);
printf("\n Enter any number ");
scanf("%d",&n);
}
fclose(fp);
fp=fopen("abc.txt","r");
fp1=fopen("qaz.txt","w");
while((n=getw(fp))!=EOF)
{
s=n*n;
putw(s,fp1);
}
fclose(fp);
fclose(fp1);

fp=fopen("qaz.txt","r");
while((n=getw(fp))!=EOF)
{
printf("\n%d",n);
}
fclose(fp);
getch();
}
8. Write a program in C to copy the content of given file say “a.txt” to another file “b.txt”.
void main()
{
FILE *fp,*fp1;
char c; // Open a.txt file and write the content
fp=fopen("a.txt","w");
clrscr();
printf("\n Enter any string ");
while((c=getchar())!='\n')
putc(c,fp);
fclose(fp); // Copy the content one file to another file
fp=fopen("a.txt","r");
fp1=fopen("b.txt","w");
while((c=getc(fp))!=EOF)
{
putc(c,fp1);
}
fclose(fp);
fclose(fp1); // Dipslat the content file a.txt
fp=fopen("a.txt","r");
while((c=getc(fp))!=EOF)
{ putchar(c);
}
fclose(fp); // Display the content file b.txt
fp=fopen("b.txt","r");
while((c=getc(fp))!=EOF)
{ putchar(c);
}
fclose(fp);
getch();
}

9. Write a program in C to create a text file. Also copy the vowels in that file to another file

10. What are the various types of files that can be created in C language? Also give different
modes in which these files can be used with proper syntax. Write a program in C
language to append some more text at end of an existed text file.

11. List out carious file operation in ‘C’. Writes a C program to count the number of
character in a file.

12. Write a program in C implement file handling with strudture such as create list, display
list, sorting list, searching list, append (new record add) and delete record

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void create();
void display();
void sorting();
void searching();
void append();
void delrec();
struct student
{
int roll;
char name[15];
};
struct student stu;
void main()
{
int n;
while(1)
{
clrscr();
printf("\n\n 1. CREATE ");
printf("\n\n 2. DISPLAY ");
printf("\n\n 3. SORTING ");
printf("\n\n 4. SEARCHING ");
printf("\n\n 5. APPEND ");
printf("\n\n 6. DELETE RECORD ");
printf("\n\n 0. EXIT ");
printf("\n\n ENTER YOUR OPTION ");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
sorting();
break;
case 4:
searching();
getch();
break;
case 5:
append();
break;
case 6:
delrec();
break;
case 0:
exit(1);
}
}
}
void create()
{
FILE *fp;
char n[15],ans='y';
int r;
fp=fopen("asd.txt","w");
while(ans=='y')
{

printf("\n Enter roll No. ");


scanf("%d",&r);
printf("\n Enter name ");
scanf("%s",n);
stu.roll=r;
strcpy(stu.name,n);
fwrite(&stu,sizeof(stu),1,fp);
fflush(stdin);
printf("\n Enter any more entry (y/n) ");
ans=getchar();
};
fclose(fp);
}
void display()
{
FILE *fp;
int i=1;
fp=fopen("asd.txt","r");
printf("\n RECORD\tROLL NUMBER NAME \n\n");
while(fread(&stu,sizeof(stu),1,fp))
{
printf("\n %d\t%d \t %s",i,stu.roll,stu.name);
i++;
}
fclose(fp);
getch();
}
void sorting()
{
struct student temp;
struct student *st;
int n,i,j,l=0;
FILE *fp;
fp=fopen("asd.txt","r");
while(fread(&st[l],sizeof(st[l]),1,fp))
l++;
fclose(fp);
printf("\n 1. FOR ROLL NUMBER ");
printf("\n 2. FOR NAME WISE ");
printf("\n Enter your option ");
scanf("%d",&n);
if(n==1)
{
for(i=0;i<l;i++)
for(j=i+1;j<l;j++)
if(st[i].roll>st[j].roll)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
}
if(n==2)
{
for(i=0;i<l;i++)
for(j=i+1;j<l;j++)
if(strcmp(st[i].name,st[j].name)>0)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
}
fp=fopen("asd.txt","w");
for(i=0;i<l;i++)
fwrite(&st[i],sizeof(st[i]),1,fp);
fclose(fp);

}
void searching()
{
char str[15];
int n,r,flag,i;
FILE *fp;
fp=fopen("asd.txt","r");
printf("\n 1. FOR ROLL NUMBER ");
printf("\n 2. FOR NAME ");
printf("\n Enter your choice ");
scanf("%d",&n);
if(n==1)
{
printf("\n Enter Roll number to searched ");
scanf("%d",&r);
flag=0;
while(fread(&stu,sizeof(stu),1,fp))
{
if(stu.roll==r)
{
printf("\n %d %s",stu.roll,stu.name);
flag=1;
break;
}
}
if(flag==0)
printf("\n Roll number is not found");
}
else
if(n==2)
{
printf("\n Enter Name to searched ");
scanf("%s",&str);
flag=0;
while(fread(&stu,sizeof(stu),1,fp))
{
if(strcmp(stu.name,str)==0)
{
printf("\n %d %s",stu.roll,stu.name);
flag=1;
break;
}
}
if(flag==0)
printf("\n Name is not found");
}
else
printf("\n Again try ");
}
void append()
{
FILE *fp;
char n[15];
int r;
fp=fopen("asd.txt","a");
printf("\n Enter roll No. ");
scanf("%d",&r);
printf("\n Enter name ");
scanf("%s",n);
stu.roll=r;
strcpy(stu.name,n);
fwrite(&stu,sizeof(stu),1,fp);
fclose(fp);
}
void delrec()
{
int r,flag=0,i;
FILE *fp,*fp1;
fp=fopen("asd.txt","r");
fp1=fopen("qaz.txt","w");
printf("\n ENTER FOR ROLL NUMBER FOR DELETED ");
scanf("%d",&r);
while(fread(&stu,sizeof(stu),1,fp))
{
if(stu.roll==r)
{
flag=1;
}
else
fwrite(&stu,sizeof(stu),1,fp1);
}
fclose(fp);
fclose(fp1);
fp1=fopen("qaz.txt","r");
fp=fopen("asd.txt","w");
while(fread(&stu,sizeof(stu),1,fp1))
fwrite(&stu,sizeof(stu),1,fp);
fclose(fp);
fclose(fp1);
if(flag==0)
{
printf("\n Roll Number Not found");
getch();
}

13. Define a structure to store employee record eg. Employee if, employee name and salary
of employee. Using this structure write a C program to create a file “employee.dat”.
There must be one record for every employeein the file. Accept the data from the user.
Using the file “employee.dat”, display the detail of employee if is entered by the user

14. Write a C program using command line argument display the content, the given file.
File name is ‘shatype.c’

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(int argc, char *argv[])
{
int i;
FILE *fp;
char str[20],c;
i=argc;
if(i==2)
{
strcpy(str,argv[1]);
fp=fopen(str,"r");
if(fp==NULL)
{
printf("\n File is not available ");
}
else
{
while((c=fgetc(fp))!=EOF)
putchar(c);
fclose(fp);
}
}
else
{
printf("\n Invalid parameter ");
getch();
}
}
15. Write a C program using command line argument copy one file to another file
Example shacopy qaz.txt asd.txt
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(int argc, char *argv[])
{
int i=argc;
FILE *fp1,*fp2;
char str1[20],char str2[20],c;
if(i==3)
{
strcpy(str1,argv[1]);
strcpy(str2,argv[2]);
fp1=fopen(str1,"r");
if(fp1==NULL)
printf("\n File is not available ");
else
{
fp2=fopen(str2,"w");
while((c=fgetc(fp1))!=EOF)
fputc(c,fp2);
fclose(fp1);
fclose(fp2);
printf("\n 1 file(s) copied. ");
}
}
else
{
printf("\n Invalid parameter ");
getch();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy