C08 Psap PPT2 21221
C08 Psap PPT2 21221
File Handling
Presented by Group C8
• 37 - Sanket Bhosle 38 - Anurag Bhujbal
• 39 - Prathamesh Bhujbal 40 - Kshitij Bhure
• 41 - Dhananjay Bile 42 - Ayush Billade
• Guide’s name – Amol Bhosle
Outline
There are a few important operations, which we will do with the help of pointers very
frequently.
(a) We define a pointer variable,
(b) assign the address of a variable to a pointer
(c) access the value at the address available in the pointer variable. This is done by
using unary operator * that returns the value of the variable located at the address
specified by its operand.
How do we Declare a Pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
1. int *a; //pointer to int
2. char *c; //pointer to char
3. float *s; //pointer to float
4. double *db; //pointer to double
Understanding pointer with help of Ex
Output:
Understanding pointers
Pointer to array:
Pointer to a function:
Pointer to structure:
Advantage of pointer
Here values of actual parameters will be copied to formal parameters and these two different parameters stores
values in different locations.
x y 20 10
x y
Output: x = 10 , y = 20
CALL BY REFERENCE
Here both the actual and formal parameter refers to same memory location .Therefore , any changes made to
the formal formal parameters will get reflected to actual parameters.
x y x y ptr1 ptr2
20 10 10 20 1000 2000
o temp = *x;
o *x = *y;
o int a = 100; o *y = temp;
o o Output
int b = 200;
o return;
o o }
o printf("Before swap, value of a : %d\n", a );
o printf("Before swap, value of b : %d\n", b );
o
o /* calling a function to swap the values */
o swap(&a, &b);
o
o printf("After swap, value of a : %d\n", a );
o printf("After swap, value of b : %d\n", b );
o
o return 0;
o }
Passing arrays to a function in C :
Output
Passing Arrays to Functions
Example :
Output :
Pass Multidimensional Arrays to a Function
Example:
Output :
Basics of Dynamic Memory
Allocation:
Static memory allocation:
Example:
Disadvantages of static memory allocation:
Size is fixed and the user cannot increase or decrease the size of the array at run time.
If values stored is less than the size specified then there will be wastage of memory.
If the values stored are more than the size specified then the program may crash or misbehave.
Dynamic memory allocation:
The process of allocating memory at the time of execution is called as dynamic memory allocation.
Built in functions :
1. malloc()
2. calloc()
3. realloc()
4. free() .
What is malloc() ?
Output:
What is realloc() ?
Output:
What is free() ?
Syntax : free(ptr)
Example
int main ()
{
int *ptr = (int*)malloc(4*size of (int));
…..
free(ptr);
}
Program:
o #include<stdio.h>
o #include<stdlib.h>
o int main(){
o int n,i,*ptr,sum=0;
Output
o printf("Enter number of elements: ");
o scanf("%d",&n);
o ptr=(int*)malloc(n,sizeof(int)); //memory allocated using malloc
o
o printf("Enter elements of array: ");
o for(i=0;i<n;++i)
o {
o scanf("%d",ptr+i);
o sum+=*(ptr+i);
o }
o printf("Sum=%d",sum);
o free(ptr);
o ptr = NULL;
o return 0;
o }
File Handling
File I/O
Opening an existing file
Reading data from the file
Writing data to the file
Moving data to a specific location on the file
Deleting the file
Types of files and syntax
1. Text Files - Normal text files whose contents can be read as plain text
2. Binary Files – Bin files that store data in binary form i.e. 0’s and 1’s which cannot be read as
plain text
Declaration of a file:
File *fptr;
Functions for File Handling
Syntax :
File *fptr
fptr = fopen(“file_name”, “mode”)
Above function accepts 2 parameters – file name and mode in which file is to be opened (Both the
parameters are strings)
Modes
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and append mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and append mode
Important Points
Input: Output:
Text file:
Example
Input: Output:
Text file:
Applications of File Handling