C-Unit-5
C-Unit-5
UNIT - V
Dynamic Memory Allocation: Introduction, Library functions –
PROBLEM SOLVING USING C malloc, calloc, realloc and free.
KCA102
UNIT-V File Handling: Basics, File types, File operations, File pointer,
MCA I YEAR, I SEM. File opening modes, File handling functions, File handling
through command line argument, Record I/O in files.
1 2
UNITED INSTITUTE OF MANAGEMENT, NAINI MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
1. malloc()
2. calloc()
3. realloc()
4. free()
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
1
2/20/2023
5 6
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
7 8
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
2
2/20/2023
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
For Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of
memory. And, the pointer ptr holds the address of the first byte in the
allocated memory. 11 12
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
3
2/20/2023
13 14
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
Continue…
calloc() method:
// Get the elements of the array “calloc” or “contiguous allocation” method in C is used to
for (i = 0; i < n; ++i) dynamically allocate the specified number of blocks of memory of the
{ specified type. it is very much similar to malloc() but has two different
ptr[i] = i + 1;
} points and these are:
It initializes each block with a default value ‘0’.
// Print the elements of the array It has two parameters or arguments as compare to malloc().
printf("The elements of the array are: "); It creates multiple block with same size.
for (i = 0; i < n; ++i)
{ Output: Syntax:
printf("%d, ", ptr[i]); Enter number of elements: 5
} Memory successfully allocated using malloc.
}
ptr = (cast-type*)calloc(n, element-size);
The elements of the array are: 1, 2, 3, 4, 5,
return 0;
} here, n is the no. of elements and element-size is the size of
each element.
15 16
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
4
2/20/2023
realloc() method:
Syntax:
Using the C realloc() function, you can add more memory size
to already allocated memory. It expands the current block ptr = realloc (ptr , newsize);
while leaving the original content as it is. realloc() in C stands
for reallocation of memory. The above statement allocates a new memory space with a
realloc() can also be used to reduce the size of the previously specified size in the variable newsize. After executing the
allocated memory. function, the pointer will be returned to the first byte of the
memory block.
The new size can be larger or smaller than the previous
Syntax: memory. We cannot be sure that if the newly allocated block
will point to the same location as that of the previous memory
ptr = realloc (ptr , newsize); block. This function will copy all the previous data in the new
region. It makes sure that data will remain safe.
19 20
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
5
2/20/2023
Example -1:
Example-2 -Dynamic Arrays in C:
#include <stdio.h>
int main () #include <stdio.h>
{ int main()
char *ptr; {
ptr = (char *) malloc(10); //memory allocation int * arr_dynamic = NULL;
strcpy(ptr, "Programming"); int elements = 2, i;
printf("String=%s, Address = %u\n", ptr, ptr); arr_dynamic = calloc(elements, sizeof(int)); //Array with 2 integer blocks
ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size for (i = 0; i < elements; i++)
strcat(ptr, " In 'C'"); arr_dynamic[i] = i;
printf(“String= %s, Address = %u\n", ptr, ptr); for (i = 0; i < elements; i++)
free(ptr); printf("arr_dynamic[%d]=%d\n", i, arr_dynamic[i]);
return 0; Note- Whenever the realloc() in C results in an unsuccessful
} operation, it returns a null pointer, and the previous data is
also freed. 21 22
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
free()-
Continue…
The free() function in C library allows you to release or
elements = 4; deallocate the memory blocks which are previously allocated by
arr_dynamic = realloc(arr_dynamic, elements * sizeof(int)); calloc(), malloc() or realloc() functions. It frees up the memory
//reallocate 4 elements blocks and returns the memory to heap. It helps freeing the
printf("After realloc\n"); OUTPUT- memory in your program which will be available for later use.
for (i = 2; i < elements; i++) Syntax-
arr_dynamic[0]=0 #include <stdio.h>
arr_dynamic[i] = i; void free(void *ptr)
arr_dynamic[1]=1 int main()
for (i = 0; i < elements; i++) {
printf("arr_dynamic[%d]=%d\n", i, arr_dynamic[i]); After realloc int* ptr = malloc(10 * sizeof(*ptr));
free(arr_dynamic); if (ptr != NULL){ *(ptr + 2) = 50;
arr_dynamic[0]=0 printf("Value of the 2nd integer is %d",*(ptr + 2));
} arr_dynamic[1]=1
}
arr_dynamic[2]=2
free(ptr);
arr_dynamic[3]=3
}
23 24
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
6
2/20/2023
SUMMERY-
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
File Handling in C-
File handling in C enables us to create, update, read,
and delete the files stored on the local file system
through our C program. The following operations can
be performed on a file.
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
7
2/20/2023
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
31 32
8
2/20/2023
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
9
2/20/2023
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
10
2/20/2023
Delete File in C-
Write string into a file : fputs() function- The remove function in C can be used to delete a file. The
function returns 0 if files is deleted successfully, other returns a
non-zero value.
Using remove() function in C, we can write a program which can
destroy itself after it is compiled and executed.
#include<stdio.h>
int main()
{
if (remove("abc.txt") == 0)
printf("Deleted successfully");
else
printf("Unable to delete the file");
return 0;
41
} 42
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
11
2/20/2023
Graphics in C-
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
void initgraph(int *graphicsDriver, int *graphicsMode, char void initgraph(int *graphicsDriver, int *graphicsMode,
*driverDirectoryPath); char *driverDirectoryPath);
Graphics Driver : It is a pointer to an integer specifying
It initializes the graphics system by loading the passed the graphics driver to be used. It tells the compiler that
graphics driver then changing the system into graphics what graphics driver to use or to automatically detect the
mode. It also resets or initializes all graphics settings like drive. In all our programs we will use DETECT macro of
color, palette, current position etc, to their default values. graphics.h library that instruct compiler for auto detection
of graphics driver.
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
12
2/20/2023
49 50
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
13
2/20/2023
Here is our first C Graphics program to draw a C Program to Draw a Circle Using C Graphics
straight line on screen.
Function Description
initgraph It initializes the graphics system by loading the passed
/* C graphics program to draw a line */ graphics driver then changing the system into graphics
#include<graphics.h> mode.
#include<conio.h> getmaxx It returns the maximum X coordinate in current graphics
int main() { mode and driver.
int gd = DETECT, gm;
getmaxy It returns the maximum Y coordinate in current graphics
/* initialization of graphic mode */ mode and driver.
initgraph(&gd, &gm, "C:\\TC\\BGI");
outtextxy It displays a string at a particular point (x,y) on screen.
line(100,100,200, 200);
getch(); circle It draws a circle with radius r and centre at (x, y).
closegraph();
closegraph It unloads the graphics drivers and sets the screen back to
return 0; text mode.
} 53 54
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
C Program to Draw a Circle Using C Graphics C program to draw rectangle and bar using graphics
#include<stdio.h> #include<stdio.h>
#include<graphics.h> #include<graphics.h>
#include<conio.h> #include<conio.h>
int main()
int main(){ {
int gd = DETECT,gm; int gd = DETECT,gm;
int x ,y ,radius=80; initgraph(&gd, &gm, "C:\\TC\\BGI");
initgraph(&gd, &gm, “BGI");
/* Initialize center of circle with center of screen */
/* Draw rectangle on screen
x = getmaxx()/2; rectangle(int left, int top, int right, int bottom);*/
y = getmaxy()/2; rectangle(150, 50, 400, 150);
outtextxy(x-100, 50, “ASHU");
/* Draw circle on screen */ /* Draw Bar on screen */
circle(x, y, radius); bar(150, 200, 400, 350);
getch();
closegraph(); getch();
return 0; closegraph();
} return 0;
55 } 56
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
14
2/20/2023
Write a Program to draw basic graphics construction like Write a Program to draw basic graphics construction like
line, circle, arc, ellipse and rectangle line, circle, arc, ellipse and rectangle
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm; initgraph (&gd,&gm,”bgi");
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
line(50,40,190,40); // line(int x1, int y1, int x2, int y2)
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165); //rectangle(int left, int top, int right, int bottom);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30); // arc(int x, int y, int start_angle, int end_angle, int radius);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30); // circle(int x, int y, int radius)
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch(); } 57 58
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
Text Functions-
setcolor(): It will set the cursor color and hence anything written
on the output screen will be of the color as per setcolor().
function prototype :
setcolor(int)
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
15
2/20/2023
setbkcolor(GREEN) Syntax :
The setbkColor function sets the current background color to void setfillstyle(int pattern, int color)
the specified color value, or to the nearest physical color if the void floodfill(int x, int y, int border_color)
device cannot represent the specified color value.
61 62
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
16
2/20/2023
Print colored message with different fonts and sizes in C. Print colored message with different fonts and sizes in C.
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
Print colored message with different fonts and sizes in C. Read Image File in C Graphics
// driver program
void main()
{
printMsg();
} Output-
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
17
2/20/2023
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
Exercise1- #include<graphics.h>
#include<conio.h>
Write a Program to draw animation using increasing circles void main()
filled with different colors and patterns. {
intgd=DETECT, gm, i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x=getmaxx()/3;
y=getmaxx()/3;
setbkcolor(WHITE);
setcolor(BLUE);
for(i=1;i<=8;i++)
{
setfillstyle(i,i);
delay(20);
circle(x, y, i*20);
floodfill(x-2+i*20,y,BLUE);
}
getch();
71 closegraph(); 72
}
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
18
2/20/2023
Page1-
Exercise3-
// C++ program to draw the moving // Move the cycle
// cycle using computer graphics for (i = 0; i < 600; i++) {
Draw a moving cycle using computer graphics #include <conio.h> // Upper body of cycle
#include <dos.h> line(50 + i, 405, 100 + i, 405);
programming in C. #include <graphics.h> line(75 + i, 375, 125 + i, 375);
#include <iostream.h> line(50 + i, 405, 75 + i, 375);
line(100 + i, 405, 100 + i, 345);
// Driver code line(150 + i, 405, 100 + i, 345);
int main() line(75 + i, 345, 75 + i, 370);
{ line(70 + i, 370, 80 + i, 370);
int gd = DETECT, gm, i, a; line(80 + i, 345, 100 + i, 345);
75 76
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
19
2/20/2023
Page2-
Exercise4-
// Road
line(0, 436, getmaxx(), 436); Write a Program to make a moving colored car
// Stone
rectangle(getmaxx() - i, 436,650 - i, 431);
using inbuilt functions.
// Stop the screen for 10 secs
delay(10);
// Clear the screen
cleardevice();
}
getch();
// Close the graph
closegraph();
}
77 78
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY
UNIT – 5 | C PROGRAMMING MR. ASHUTOSH PANDEY UNIT – 4 | C PROGRAMMING MR. ASHUTOSH PANDEY
20