0% found this document useful (0 votes)
21 views

File Concept

Uploaded by

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

File Concept

Uploaded by

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

File handling

• File is a collection of data.


• By using file concept we can perform operations on large
data.
• Files are two types
–Text File
–Binary File
• File handling in C enables us to create, update, read, and
delete the files stored on the local file system through our C
program.
• Text file
– A Text file represents a sequence of characters that can be
processed sequentially.
– The text files are in human readable form and they can be
created and read using any text editor.
– Example: .txt, .c, .java
• Binary file
– Binary file is a sequence of zeros and ones.
– The binary files are interpreted and understood by a
computer system.
– Example: .exe, .dll
Operations on a file
• The following operations can be performed on a file.
– Creation of the new file
– Opening an existing file
– Reading from a file
– Writing to a file
– Moving to a specific location in a file(Seeking)
– Closing a file
File pointer
• Before performing any operation on a file we should declare a
file pointer variable.
• Syntax:
data-type *variable name;
• Example:
FILE *fp;
• fp is a file pointer variable and FILE is the derived data type.
Text File opening modes
• “r”: reading from the existing file.
• “w”: writing to the new file or overwrite existing file.
• “a”: adding new contents to the end of the existing file.
• “r+”: reading existing contents, writing new contents, modifying
existing contents of the file.
• “w+”: writing new contents, reading them back and modifying
existing contents of the file.
• “a+”: reading existing contents, appending new contents to end of
file. Cannot modify existing content.
Binary File opening modes

• “rb”: reading from the binary file


• “wb”: writing to a binary file
• “ab”: append to an existing binary file
• “rb+”: open a binary file for read/write
• “wb+”: create a binary file for read/write
• “ab+”: append or create a binary file for read/write
Library functions for file handling
Opening and closing a file
• To read or write information from/to a file on a disk, we must open
the file.
• The function fopen() is used to create a new file or to open an
existing file in a specific mode.
• Syntax:
– FILE *fp;
– fp=fopen(“file_name”,”mode”);
• fopen() returns the address of the FILE structure, which is collected
in structure pointer fp.
• The fclose() is used to close an already opened file.
• Syntax:
– fclose(file_pointer_variable);
– fclose(fp);
Program to display the contents of a file on standard
output device
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp; //file pointer
char ch;
clrscr();
fp=fopen("struct_2.c","r"); //opening struct_2.c file
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch); //printing the content on console
}
printf("\n");
fclose(fp);
getch();
return 0;
}
Output
Program to Copy one file to another file, replacing all lowercase
characters to uppercase
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char ch;
clrscr();
fp1=fopen("sample.txt","r");
fp2=fopen("output.txt","w");
while((ch=getc(fp1))!=EOF)
{
ch=toupper(ch);
putc(ch,fp2);
}
printf("File is copied\n");
fclose(fp1);
fclose(fp2);
getch();
}
Output
Program to find frequency of characters
#include<stdio.h>
#include<conio.h>
int main()
{
int c = 0, count[26] = {0}, x;
char ch;
FILE *fp=fopen("sample1.txt","r");
clrscr();
while((ch=fgetc(fp))!=EOF)
{
if (ch>='a'&&ch<= 'z')
{
x = ch- 'a';
count[x]++;
}
}
for (c = 0; c < 26; c++)
printf("%c occurs %d times \n", c + 'a', count[c]);
getch();
return 0;
}
Program to merge two files into a third file
#include <stdio.h> fclose(fp1);
#include<conio.h> fclose(fp2);
int main() fclose(fp3);
{ getch();
FILE *fp1 = fopen("file1.txt", "r"); return 0;
FILE *fp2 = fopen("file2.txt", "r"); }
FILE *fp3 = fopen("file3.txt", "w");
char c;
clrscr();
// Copy contents of file1 to file3
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
fputc(‘\n’,fp3);
// Copy contents of file2 to file3
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into
file3.txt");
Output
fscanf and fprintf functions

• fscanf(): The fscanf() function is used to read set of characters


from text file.
• Syntax:
– fscanf(fp,"format specifier",arguments);

• fprintf(): The fprintf() function is used to write set of characters


into file.
• Syntax:
– fprintf(fp,"formated specifier",arguments);
Program to illustrate fprintf and fscanf functions
#include<stdio.h> while(fscanf(fp, "%s", b)!=EOF)
#include<conio.h> {
void main() printf("%s ", b );
{ }
FILE *fp; fclose(fp);
char b[100]; getch();
char name[10]; }
int age;
clrscr();
printf("Enter name and age:\n");
scanf("%s %d",name,&age);
//opening the file in write mode
fp=fopen("sample_file.txt","w");
fprintf(fp,"%s%d",name,age);
fclose(fp);
//opening file in read mode
fp = fopen("sample_file.txt", "r");
Why binary files?
• I/O operations are faster with binary files
• Binary files are much smaller in size than text files
• Some data cannot be converted to characters.
fread and fwrite functions
• fread(): This function is used to read a block of data from a file.
• Syntax:
– int fread(void*ptr, int size, int n, FILE*fp);
• fwrite(): This function is used to write a block of data into a file.
• Syntax:
– int fwrite(void *ptr, int size, int n, FILE*fp);

• 1st parameter: the memory address of the value to be read and


write
• 2nd parameter: no. of bytes of data block
• 3rd parameter: total no. of blocks to read/write
• 4th parameter: the file pointer
Program to illustrate binary file using fread() and fwrite()
#include<stdio.h> while(fread(&e1,sizeof(e1),1,fp)==1)
#include<conio.h> {
struct Emp printf("employee details: %s\n %d\n %.2f\
{ n",e1.ename,e1.eid,e1.esal);
char ename[10]; }
int eid; fclose(fp);
float esal; getch();
}; }
void main()
{
struct Emp e,e1;
FILE *fp;
fp=fopen("emp.dat","wb");
clrscr();
printf("enter employee name, id and salary:\n");
scanf("%s%d%f", e.ename, &e.eid, &e.esal);
fwrite(&e,sizeof(e),1,fp);
fclose(fp);
fp=fopen("emp.dat","rb");
File positioning functions
• File positioning functions are used for accessing the random
data from the file.
• File positioning functions are :
– rewind();
– ftell();
– fseek();

• rewind(): The rewind() function is used to set the file pointer


to the beginning of the file.
• Syntax:
– FILE *fp;
– void rewind(fp);
Program for rewind()
#include <stdio.h> rewind(fp);
#include<conio.h> printf("\n");
int main () while((ch=getc(fp))!=EOF)
{ {
char str[] = "welcome to kmit"; printf("%c",ch);
FILE *fp; }
char ch; fclose(fp);
clrscr(); getch();
fp = fopen( "file.txt" , "w" ); return 0;
fprintf(fp,"%s",str); }
fclose(fp);
fp = fopen( "file.txt" , "r" );
while((ch=getc(fp))!=EOF)
{
printf("%c",ch);
}
ftell and fseek functions
• ftell(): The ftell() gives the current position of the file pointer from the
beginning of the file.
• Syntax:
FILE *fp;
long ftell(fp);

• fseek(): The function fseek() is used to set the file pointer at the
specified position.
• The file pointer can be moved backwards or forward any number of
bytes.
• Syntax:
int fseek(FILE *fp, long offset, int start_point);
Program on fseek and ftell functions

#include <stdio.h>
#include<conio.h>
int main ()
{
FILE *fp;
int len;
clrscr();
fp = fopen("file.txt", "r");
fseek(fp, 0, 2); //file pointer fp moves to the end of file
len = ftell(fp);
fclose(fp);
printf("Total size of file.txt = %d bytes\n", len);
getch();
return(0);
}

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