Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CCCS - 206 Unit - 2 Structures, Unions and File Handling

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

Unit - 2 Structures,

Unions and File


Handling
Prof. Nehal Sheth
DNV International Education Academy
Basics of Structures
In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that an entity has all the
information of one type only. It can have different attributes of different data types. For example, an entity Student may
have its name (string), roll number (int), marks (float). To store such type of information regarding an entity student, we
have the following approaches:

● Construct individual arrays for storing names, roll numbers, and marks.
● Use a special data structure to store the collection of different data types.
Syntax of Structure
The ,struct keyword is used to define the structure. Let's see the syntax to define the structure in c.
Example of Structure
Let's see the example to define a structure for an entity employee in c.
Memory Allocation of Structure
Declaring a Structure Variable
We can declare a variable for the structure so that we can access the member of the structure easily. There are two
ways to declare structure variable:

1. By struct keyword within main() function


2. By declaring a variable at the time of defining the structure.

1st Way : 2nd Way :

Write Following code in main() function.


Accessing Members of the Structure
There are two ways to access structure
members:

1. By . (member or dot operator)


2. By -> (structure pointer operator)
Structure Example
Structure Example
Structures and Functions
Return Struct from a Function
Arrays of Structures
An array of structures in C can be defined as the collection of multiple structures variables where each variable contains
information about different entities. The array of structures in C are used to store information about multiple entities of
different data types. The array of structures is also known as the collection of structures.
Arrays of Structures
Nested Structures
C provides us the feature of nesting one structure within another structure by using which, complex data types are
created.
2) Embedded structure

Nested Structures The embedded structure enables us to declare the structure inside the
structure. Hence, it requires less line of codes but it can not be used in
multiple data structures. Consider the following example.
The structure can be nested in the following ways.

1. By separate structure
2. By Embedded structure

1) Separate structure

Here, we create two


structures, but the dependent
structure should be used
inside the main structure as a
member.
Nested Structures Example
Structure and Arrays
An array is collection of items stored at contiguous memory locations.

A structure is a user defined data type in C/C++. A


structure creates a data type that can be used to
group items of possibly different types into a single
type.
Difference between Structures and Arrays
ARRAY STRUCTURES

Array refers to a collection consisting of elements of Structure refers to a collection consisting of elements of
homogeneous data type. heterogeneous data type.

Array uses subscripts or “[ ]” (square bracket) for Structure uses “.” (Dot operator) for element access
element access

Instantiation of Array objects is not possible. Instantiation of Structure objects is possible.

Array declaration is done simply using [] and not any Structure declaration is done with the help of “struct”
keyword. keyword.

Arrays is a non-primitive datatype Structure is a user-defined datatype.

Array traversal and searching is easy and fast. Structure traversal and searching is complex and slow.

Array elements are accessed by their index number Structure elements are accessed by their names using
using subscripts. dot operator.
Pointers to Structures
Pointers to Structures
Nested Structures
Unions
Unions can be defined as a user-defined data type which is a
collection of different variables of different data types in the same
memory location. The union can also be defined as many
members, but only one member can contain a value at a particular
point in time.

Union is a user-defined data type, but unlike structures, they share


the same memory location.
TypeDefs
The C programming language provides a
keyword called typedef, which you can
use to give a type a new name.
Following is an example to define a term
BYTE for one-byte numbers

typedef unsigned char BYTE;


Introduction to File Handling
File Handling is the storing of data in a file using a program. In C programming language, the programs
store results, and other data of the program to a file using file handling in C. Also, we can extract/fetch
data from a file to work with it in the program.

The operations that you can perform on a File in C are −

● Creating a new file


● Opening an existing file
● Reading data from an existing file
● Writing data to a file
● Moving data to a specific location on the file
● Closing the file
File Access Modes
Types of Files
When dealing with files, there are two types of files you should know about:

2. Binary files
1. Text files

2. Binary files Binary files are mostly the .bin files in


your computer.
1. Text files

Text files are the normal .txt files. You can easily create text Instead of storing data in plain text,

files using any simple text editors such as Notepad. they store it in the binary form (0's

and 1's).
When you open those files, you'll see all the contents within

the file as plain text. You can easily edit or delete the contents. They can hold a higher amount of

data, are not readable easily, and


They take minimum effort to maintain, are easily readable, and
provides better security than text
provide the least security and takes bigger storage space.
files.
Writing to a Text File
Reading from a Text File
Writing to a Binary File
Reading from a Binary File
Functions for File Handling
C Printf and Scanf
Writing File : fprintf() function Reading File : fscanf() function
The fprintf() function is used to write set of characters The fscanf() function is used to read set of characters
into file. It sends formatted output to a stream. from file. It reads a word from the file and returns EOF at
the end of file.
C Printf and Scanf
C fputc() and fgetc()
Writing File : fputc() function Reading File : fgetc() function
The fputc() function is used to write a single The fgetc() function returns a single character from the
character into file. It outputs a character to a file. It gets a character from the stream. It returns EOF
stream. at the end of file.
C fputs() and fgets()
Writing File : fputs() function Reading File : fgets() function
The fputs() function writes a line of characters into The fgets() function reads a line of characters from file.
file. It outputs string to a stream. It gets string from a stream.
C fseek()
The fseek() function is used to set the file pointer to the specified
offset. It is used to write data into file at desired location.

You might also like