[go: up one dir, main page]

0% found this document useful (0 votes)
14 views22 pages

File Handling in C

The document provides an overview of file handling in C, including the importance of storing data in permanent storage and the types of files (text and binary). It outlines the major file operations such as creating, opening, closing, and reading/writing files, as well as the use of file pointers for communication between the program and files. Additionally, it discusses file opening modes and the necessity of closing files after operations.

Uploaded by

23ec043
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)
14 views22 pages

File Handling in C

The document provides an overview of file handling in C, including the importance of storing data in permanent storage and the types of files (text and binary). It outlines the major file operations such as creating, opening, closing, and reading/writing files, as well as the use of file pointers for communication between the program and files. Additionally, it discusses file opening modes and the necessity of closing files after operations.

Uploaded by

23ec043
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/ 22

FILE HANDLING IN C

Dr. Miral Desai


Department of EC Engineering,
FTE, CSPIT, CHARUSAT
FILE HANDLING IN C

• Input and Output operations performed so far were done through Keyboard and Screen
only. Data entered through keyboard and output performed on the screen.
• After termination of program, all the entered data is lost as primary memory is volatile.
• If the data has to be used at later stage, It must be stored in permanent storage device.
• There are many I/O library Functions through which data can be stored on the disk or
any secondary storage device.
• A Data file is a collection of related data placed on a Secondary storage device.
• File Handling in C can be broadly categorized in two types:
• High Level Files (Standard Files or Stream Oriented Files) - Managed by Library Functions
• Low level Files (System Oriented Files) - Managed by System Calls
TEXT & BINARY STREAMS

• Input and Output in C is performed by the logical data streams, which can be connected
to different input-output devices.
• The concept of streams makes input and output on different devices uniform.
• File management is done by the execution environment, and streams
TYPES OF FILES

• When dealing with files, there are two types of files you should know about:
• Text Files
• Binary Files
• Text Files
• Text files are the normal .txt files. It can be easily created using any simple text editors such
as Notepad.
• They take minimum effort to maintain, are easily readable, and provide the least security
and takes bigger storage space.
• Binary Files
• Binary files are mostly the .bin files in your computer.
• They can hold a higher amount of data, are not readable easily, and provides better security
than text files.
FILE OPERATIONS

• In C, you can perform four major operations on files, either text or binary:
• Creating a new file
• Opening an existing file
• Closing a file
• Reading from and writing information to a file
FILE HANDLING

• When working with files, It need to declare a pointer of type file.


• This declaration is needed for communication between the file and the program.
• A file pointer is a reference to a particular position in the opened file. It is used in file
handling to perform all file operations such as read, write, close, etc.
• We use the FILE macro to declare the file pointer variable. The FILE macro is defined
inside <stdio.h> header file.
OPENING A FILE IN C - FOR CREATION AND EDIT (1)

• Opening a file is performed using the fopen() function.


• The syntax for opening a file in standard I/O is:

• Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram. The
first function creates a new file named newprogram.txt and opens it for writing as per
the mode 'w'.
• The writing mode allows you to create and edit (overwrite) the contents of the file.
OPENING A FILE IN C - FOR CREATION AND EDIT (2)

• Now let's suppose the second binary file oldprogram.bin exists in the location
E:\cprogram. The second function opens the existing file for reading in binary mode
'rb'.
• The reading mode only allows you to read the file, you cannot write into the file.
FILE OPENING MODES (1)
FILE OPENING MODES (2)
FILE OPENING MODES (3)
ERRORS IN OPENING FILE
CLOSING FILE IN C

• Any file must be closed after reading/writing.


• Closing a file is performed using the fclose() function.

• Here, fptr is a file pointer associated with the file to be closed.


FPUTC()
FGETC()
FPRINTF()
FSC ANF()
FWRITE() (1)
FWRITE() (2)
FREAD()
FSEEK() & FTELL()
COMMAND LINE ARGUMENTS

You might also like