Professional Documents
Culture Documents
Unit#5
Unit#5
Part C: Creating a data file, Opening and closing a data file, Various I/O
operations on data files: Storing data or records in file, adding records,
Retrieving, and updating Sequential file/random file.
Part A: Structure and Unions: Introduction, Declaration, Difference,
Application, Nested structure, self-referential structure, Array of structures,
Passing structure in function.
DEFINITION OF STRUCTURES AND UNIONS
A structure in C is a user-defined data type that allows you to group
variables of different data types under a single name. Each variable
Structure within the structure is called a member, and these members can have
different data types. The structure provides a way to organize related
information into a cohesive unit.
The primary purpose of files is to provide a mechanism for data persistence. When a
program terminates, the data stored in variables is lost, but files allow data to be stored on
non-volatile storage devices like hard drives.
C supports various types of files, each with its own characteristics and use cases. These
include text files (human-readable) and binary files (contains non-textual data).
File operations involve opening, reading, writing, and closing files. The stdio.h library
provides functions like fopen, fread, fwrite, and fclose for these operations.
EXAMPLE Step 1
Step 2
Step 3
In this program:
• The input file "input.txt" is opened in read mode.
• The program reads each line from the input file
using fgets and prints it to the console.
• The line count is incremented for each line read.
• The total line count is printed to the console.
• An output file "output.txt" is created in write mode,
and the line count is written to it using fprintf.
• Both files are closed using fclose.
I/O STREAMING AND BUFFERING
In C programming, Input/Output (I/O) operations are facilitated through the concept of I/O
streaming and buffering. These concepts are essential for efficient data transfer between a
program and external devices like files or the console.
I/O streaming involves the continuous flow of data between the program and external
devices, such as files or the console. It treats input and output as a sequence of data
streams.
• Stream: A stream is a flow of data, and in C, there are two main types: input stream and
I/O Streaming
output stream.
• Input Stream (stdin): Standard input stream, typically representing keyboard input.
• Output Stream (stdout and stderr): Standard output and standard error streams,
typically representing display output.
Functions for I/O Streaming:
• printf: Outputs formatted data to the standard output (stdout).
• scanf: Reads formatted data from the standard input (stdin).
I/O STREAMING AND BUFFERING
Buffering is a mechanism that involves temporarily storing data in memory (a buffer)
before it is read from or written to an external device. This temporary storage helps
optimize I/O operations.
Advantages:
• Reduced Number of I/O Operations: Buffering minimizes the number of direct read and
Buffering
File operations in C, such as fread and fwrite, often involve buffering. The library provides
functions that automatically handle buffering. stdio.h
TYPES OF FILE
In C programming, files can be classified into different types based on their characteristics
and how they are accessed. The three main types of files are Indexed Files, Sequential Files,
and Random Files.
Indexed files are files that have an associated index, which allows for faster access to
specific records.
Indexed Files
Characteristics:
• Each record in the file has a unique key or identifier.
• An index is maintained, mapping keys to the corresponding record positions.
• Enables quick retrieval of records based on the key.
Advantages:
• Efficient for random access operations.
• Well-suited for applications requiring frequent search and retrieval of specific records.
TYPES OF FILE
Sequential files are files where records are stored and accessed in a linear or sequential
fashion.
Sequential Files
Characteristics:
• Records are stored one after the other in a sequence.
• Access to records is typically sequential; one must read preceding records to reach the
desired record.
• Well-suited for tasks that involve processing data sequentially.
Advantages:
• Simple and straightforward to implement.
• Well-suited for applications where data processing follows a specific order.
TYPES OF FILE
Random files, also known as Direct Access Files, allow direct access to any record using a
unique identifier or key.
Characteristics:
Random Files
• Each record has a unique identifier, and records can be accessed directly without
reading all preceding records.
• Provides fast access to specific records based on a key.
• Supports both random and sequential access.
Advantages:
• Efficient for applications requiring direct access to specific records.
• Suitable for scenarios where records need to be accessed out of order.
Part C: Creating a data file, Opening and closing a data file, Various I/O
operations on data files: Storing data or records in file, adding records,
Retrieving, and updating Sequential file/random file.
EXAMPLE: SEQUENTIAL FILE
Step 2
Step 1
EXAMPLE: RANDOM FILE
Step 1 Step 2
EXAMPLE: RANDOM FILE
Step 4
Thank you.. ☺