0% found this document useful (0 votes)
4 views11 pages

Unit 8 File Handling

The document provides an overview of file management in C programming, detailing the necessity of file handling for storing and retrieving large amounts of data. It outlines various functions for file operations such as opening, reading, writing, and closing files, along with their syntax and examples. Additionally, it explains different file opening modes and includes sample C programs for reading and copying files.

Uploaded by

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

Unit 8 File Handling

The document provides an overview of file management in C programming, detailing the necessity of file handling for storing and retrieving large amounts of data. It outlines various functions for file operations such as opening, reading, writing, and closing files, along with their syntax and examples. Additionally, it explains different file opening modes and includes sample C programs for reading and copying files.

Uploaded by

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

UNIT-8

FILE MANAGEMENT

1
File Handling in C
▪ In programming, we may require some specific input data to be
generated several numbers of times.
▪ Sometimes, it is not enough to only display the data on the
console. The data to be displayed may be very large, and only a
limited amount of data can be displayed on the console, and since
the memory is volatile, it is impossible to recover the
programmatically generated data again and again.
▪ However, if we need to do so, we may store it onto the local file
system which is volatile and can be accessed every time. Here,
comes the need of file handling in C.
▪ File handling in C enables us to create, update, read, and delete
the files stored on the local file system through our C program.
Functions for file handling
▪ There are many functions in the C library to open, read, write,
search and close the file.

Syntax Description
fp=fopen(file_na This statement opens the file and assigns an identifier to the FILE type
me, mode); pointer fp.

Example: fp = fopen("printfile.c","r");
fclose(filepoint Closes a file and release the pointer.
er);
Example: fclose(fp);
fprintf(fp, Here fp is a file pointer associated with a file. The control string
“control string” contains items to be printed. The list may includes variables,
,list); constants and strings. write data into the file.

Example: fprintf(fp, "%s %d %c", name, age, gender);


Functions for file handling
Syntax Description
fscanf(fp, Here fp is a file pointer associated with a file. The control string
“control strin contains items to be printed. The list may includes variables,
g”, constants and strings. reads data from the file.
list);
Example: fscanf(fp, "%s %d", &item, &qty);
int getc( getc() returns the next character from a file referred by fp; it
FILE *fp); require the FILE pointer to tell from which file. It returns EOF for
end of file or error. reads a character from file.

Example: c = getc(fp);
int putc(int c putc() writes or appends the character c to the FILE fp. If a putc
, function is successful, it returns the character written, EOF if an error
FILE *fp); occurs. writes a character into the file.

Example: putc(c, fp);


Functions for file handling
Syntax Description
int getw( getw() reads an integer value from FILE pointer fp and returns an
FILE *pvar); integer.
Example: i = getw(fp);
putw(int, putw writes an integer value read from terminal and are written to the
FILE *fp); FILE using fp.

Example: putw(i, fp);


EOF EOF stands for “End of File”. EOF is an integer defined in <stdio.h>

Example: while(ch != EOF)


Syntax Description
fseek(FILE *fp, fseek() function is used to move the file position to a desired
long offset, location within the file. fp is a FILE pointer, offset is a value of
int position); datatype long, and position is an integer number.

Example: /* Go to the end of the file, past the last character of the
file */
fseek(fp, 0L, 2);
long ftell(FILE * ftell takes a file pointer and returns a number of datatype long,
fp); that corresponds to the current position. This function is useful in
saving the current position of a file.

Example: /* n would give the relative offset of the current position.


*/
n = ftell(fp);
rewind(fp); rewind() takes a file pointer and resets the
position to the start of the file.

Example: /* The statement would assign 0 to n


because the file position has been set to the start
of the file by rewind. */

rewind(fp);
Opening File: fopen()
▪ We must open a file before it can be read, write, or update. The
fopen() function is used to open a file. The syntax of the fopen() is
given below.
▪ Syntax:: FILE *fopen( const char * filename, const char * mode );
▪ The fopen() function accepts two parameters:
• The file name (string). If the file is stored at some specific location, then we
must mention the path at which the file is stored. For example, a file name
can be like "c://some_folder/some_file.ext".
• The mode in which the file is to be opened. It is a string.

File Opening Modes
▪ We can perform different operations on a file based on the file
opening modes

Mode Description

Open the file for reading only. If it exists, then the file is opened with the current
r
contents; otherwise an error occurs.
Open the file for writing only. A file with specified name is created if the file does
w
not exists. The contents are deleted, if the file already exists.
Open the file for appending (or adding data at the end of file) data to it. The file is
a opened with the current contents safe. A file with the specified name is created if
the file does not exists.
r+ The existing file is opened to the beginning for both reading and writing.

w+ Same as w except both for reading and writing.


a+ Same as a except both for reading and writing.
Write a C program to display content of a given
file.(Fileread.c)
#include<stdio.h>

void main( )
{
FILE *fp ;
char ch ;
fp = fopen("hello.txt","r") ;
while ( 1 )
{
ch = fgetc ( fp ) ;

if ( ch == EOF )
break ;

printf("%c",ch) ;
}
fclose (fp ) ;
}
Write a C program to copy a given
file.(filecopy.c)
#include<stdio.h>

void main( )
{
FILE *fp ,*fp1;
char ch ;
fp = fopen("hello.txt","r") ;

fp1=fopen("copy.txt","w");

do
{
ch = fgetc ( fp ) ;

fputc(ch,fp1);

}while(ch!=EOF);
fclose (fp ) ;
fclose(fp1);
}
Example
▪ File handling Example of fprintf and fsacnf.(File1.c)
▪ File handling Example of fputs and fgets.(File2.c)
▪ file named "data" contains series of integers. C program to read
numbers from file and write odd and even numbers in file named
"odd" and "even" respectively.(File3.c)

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