Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Dr.

Pawan Kumar Verma


UNIT#5 Associate Professor
CSE Department
AGENDA
Part A: Structure and Unions: Introduction, Declaration, Difference,
Application, Nested structure, self-referential structure, Array of structures,
Passing structure in function.

Part B: Files: Introduction, concept of record, I/O Streaming and Buffering,


Types of Files: Indexed file, sequential file and random file.

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.

A union is similar to a structure in that it allows you to group variables


of different data types under a single name. However, unlike structures,
unions share the same memory location for all their members. This
Unions
means that only one member of the union can be used at a time, and
changing the value of one member will affect the values of other
members.
DEFINITION OF STRUCTURES AND UNIONS
Syntax for declaring Syntax for declaring
a structure a union

In the example above, a union named "Status"


In the example above, a structure named
is defined with two members, 'errorCode' and
"Point" is defined with two members, 'x'
'message'. However, only one of these
and 'y', both of type int.
members should be used at any given time.
STRUCTURES: EXAMPLE
UNION: EXAMPLE
NESTED STRUCTURES
Structures can be nested inside other structures.
SELF-REFERENTIAL STRUCTURES
A structure can have a member that is a pointer to the same type of structure.
ARRAY OF STRUCTURES
In C programming, We can define an array of
structures to store multiple records of the
same structure type. Each element of the
array is a structure instance.
PASSING STRUCTURES TO FUNCTIONS
Passing by Value vs. Passing by Reference:
• Passing by value creates a copy of the structure.
• Passing by reference uses pointers, allowing modifications to the original structure.
AGENDA

Part B: Files: Introduction, concept of record, I/O Streaming and Buffering,


Types of Files: Indexed file, sequential file and random file.
DEFINITION OF FILES
In C programming, a file is a logical unit for storing and retrieving data. It serves as a means
of persistent data storage beyond the lifetime of a program.

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

write operations, which can be slower.


• Enhanced Performance: By dealing with data in larger chunks, buffering improves the
overall efficiency of I/O operations.
• Optimized Disk Access: In the case of file I/O, buffering reduces the frequency of
interactions with the disk, making I/O operations more efficient.

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.. ☺

You might also like