0% found this document useful (0 votes)
11 views5 pages

Unit - 10

PPS UNIT 10

Uploaded by

try.vishal0602
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)
11 views5 pages

Unit - 10

PPS UNIT 10

Uploaded by

try.vishal0602
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/ 5

PPS – 3110003 Prepared By: Prof.

Jigar Dalvadi

UNIT-10 - File management


Introduction to file management and its functions
 File handling in C stores data of our program to our local storage which can be used at
any time because as the execution of a program completes our data is lost. Therefore
here comes the need to save our data in any file form - text files or binary files.
 All functions related to file management are in <stdlib.h> header file.

The need for file handling in C is because -

 It saves time as we can easily access any part/outcome of the code whenever required.
 It helps in preserving the data or information for reusability.
 There is no need to worry about storage.
 And without loss of data, we can easily transfer the contents of a file.

Types of Files in C

We will be working with two types of files: -

 Text file
 Binary file
 Following are the most important file management functions available in „C,‟

function purpose
fopen () Creating a file or opening an existing file
fclose () Closing a file
fprintf () Writing a block of data to a file
fscanf () Reading a block data from a file
getc () Reads a single character from a file
putc () Writes a single character to a file
getw () Reads an integer from a file
putw () Writing an integer to a file
fseek () Sets the position of a file pointer to a specified location

How to create a file


 Whenever you want to work with a file, the first step is to create a file. A file is nothing
but space in a memory where data is stored.
 It is a library function present in the <stdlib.h> header file.

Syntax to create a file in a ‘C’

FILE *fp;
fp = fopen ("file_name", "mode");
1|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

 fopen is a standard function which is used to open a file.


 If the file is not present on the system, then it is created and then opened.
 If a file is already present on the system, then it is directly opened using this function.
 fp is a file pointer which points to the type file.

File Mode Description


r Open a file for reading. If a file is in reading mode, then no data is
deleted if a file is already present on a system.
w Open a file for writing. If a file is in writing mode, then a new file
is created if a file doesn‟t exist at all. If a file is already present on
a system, then all the data inside the file is truncated, and it is
opened for writing purposes.
a Open a file in append mode. If a file is in append mode, then the
file is opened. The content within the file doesn‟t change.
r+ open for reading and writing from beginning
w+ open for reading and writing, overwriting a file
a+ open for reading and writing, appending to file

How to Close a file


 One should always close a file whenever the operations on file are over. It means the
contents and links to the file are terminated. This prevents accidental damage to the file.
 „C‟ provides the fclose function to perform file closing operation.

syntax of fclose
 fclose (file_pointer);

Example

FILE *fp;
fp = fopen ("data.txt", "r");
fclose (fp);

Writing to a File
 In C, when you write to a file, newline characters „\n‟ must be explicitly added.

The stdio library offers the necessary functions to write to a file:

 fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer.


 fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer.

2|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

 fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by


file_pointer. The string can optionally include format specifiers and a list of variables
variable_lists.
 fputc() Function:

 fputs() function:

 fprintf()Function:
3|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Reading Data From an Existing File


 To read data from an existing file we will use “r” mode in file opening. To read the file
character by character we use getc() and to read line by line we use fgets().

Moving Data to a Specific Location on the File


 To put the file pointer to a specific place we use fseek(). With the help of it we can write
data at whatever location we want in the file.

sSyntax -
4|P ag e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

fseek(FILE stream, long int offset, int whence)

 file stream - pointer to the file. As in the example file is the pointer to the file
object "myfile.txt".
 offset - This is the number of bytes that need to be offset/skip from whence.
 SEEK_SET - sets the pointer at the beginning of the file. SEEK_CUR - sets the pointer
at the current position of the file SEEK_END - sets the pointer at the end of the file.

5|P ag e

You might also like