SlideShare a Scribd company logo
FILE HANDLING, DYNAMIC MEMORY
ALLOCATION
UNIT- III
Fundamental of Computer
programming -206
By- Er. Indrajeet Sinha , +919509010997
UNIT-III
 Array of Structure and pointers
 File Handling
 Opening & Closing file
 fread(), fwrite()
 Dynamic Memory Allocation
2
ARRAY OF STRUCTURE
AND POINTERS
Lecture no.- 28,
UNIT- III
By- Er. Indrajeet Sinha , +919509010997
3.1.1 ARRAY OF STRUCTURES
 Definition:
Structure is used to store the information of One particular
object but if we need to store such 100 objects then Array of
Structure is used.
 Example :
struct Bookinfo
{
char[20] bname;
int pages;
int price;
}
Book[100];
4
1.Here Book structure is used to
Store the information of one Book.
2. In case if we need to store the
Information of 100 books then
Array of Structure is used.
3. b1[0] stores the Information of
1st Book , b1[1] stores the
information of 2nd Book and So on
We can store the information of 100
books.
By- Er. Indrajeet Sinha , +919509010997
3.1.2 POINTERS TO STRUCTURES
 Pointer to Structure in C Programming
 Address of Pointer variable can be obtained using ‘&’operator.
 Address of such Structure can be assigned to the Pointer
variable .
 Pointer Variable which stores the address of Structure must
be declared as Pointer to Structure .
 Pointer to Structure Syntax :
struct student_database
{
char name[10];
int roll;
int marks;
}stud1;
struct student_database *ptr;
ptr = &stud1; How to Access Structure Members?
5
By- Er. Indrajeet Sinha , +919509010997
Simple program without Pointer variable:
#include <stdio.h>
int main()
{
struct student database
{
char name[10];
int roll; int marks;
}stud1;
stud1.roll = 10;
stud1.marks = 90;
printf("Roll Number : %d",stud1.roll);
printf("Marks of Student : %d",stud1.marks);
return 0;
}
6
Output:
Roll Number : 10
Marks of Student : 90
By- Er. Indrajeet Sinha , +919509010997
 Simple program with Pointer variable:
#include <stdio.h>
int main(int argc, char *argv[])
{
struct student_database
{
char name[10];
int roll;
int marks;
}stud1 = {"Pritesh",90,90};
struct student_database *ptr;
ptr = &stud1;
printf("Roll Number : %d",(*ptr).roll);
printf("Marks of Student : %d",(*ptr).marks);
return 0;
}
7
Output:
Roll Number : 90
Marks of Student : 90
By- Er. Indrajeet Sinha , +919509010997
3.1.3 ARRAY OF POINTERS
 Array of pointers
8
FILE HANDLING
Lecture no.- 29,
UNIT- III
By- Er. Indrajeet Sinha , +919509010997
3.2.1 INTRODUCTION OF FILE
HANDLING
 Drawbacks of Traditional I/O System.
1. Until now we are using Console Oriented I/O functions.
“Console Application” means an application that has a text-
based interface. (black screen window))
2. Most applications require a large amount of data , if this
data is entered through console then it will be quite
time consuming task.
3. Main drawback of using Traditional I/O :- data is
temporary (and will not be available during re-execution )
10
By- Er. Indrajeet Sinha , +919509010997
 File handling in C :
1. New way of dealing with data is file handling.
2. Data is stored onto the disk and can be retrieve whenever
require.
3. Output of the program may be stored onto the disk
4. In C we have many functions that deals with file handling
5. A file is a collection of bytes stored on a secondary
storagedevice (generally a disk)
Collection of byte may be interpreted as –
 Single character
 Single Word
 Single Line
 Complete Structure.
11
By- Er. Indrajeet Sinha , +919509010997
12
By- Er. Indrajeet Sinha , +919509010997
3.2.2 OPENING & CLOSING FILE
 Opening Files
We can use the fopen( ) function to create a new file or to
open an existing file. This call will initialize an object of the
type FILE, which contains all the information necessary to
control the stream. The prototype of this function call is as
follows −
FILE *fopen( const char * filename, const char * mode );
Note:- Here, filename is a string literal, which will use to
name of our file, and access mode can have any one.
13
By- Er. Indrajeet Sinha , +919509010997
Mode Description
r Opens an existing text file for reading purpose.
w Opens a text file for writing. If it does not exist, then a new file
is created. Here your program will start writing content from
the beginning of the file.
a Opens a text file for writing in appending mode. If it does not
exist, then a new file is created. Here your program will start
appending content in the existing file content.
r+ Opens a text file for both reading and writing.
w+ Opens a text file for both reading and writing. It first truncates
the file to zero length if it exists, otherwise creates a file if it
does not exist.
a+ Opens a text file for both reading and writing. It creates the
file if it does not exist. The reading will start from the
beginning but writing can only be appended. 14
By- Er. Indrajeet Sinha , +919509010997
If you are going to handle binary files, then we will use
following access modes instead of the above mentioned
ones
15
Mode Description
rb Opens an existing text file for reading purpose.
wb Opens a text file for writing. If it does not exist, then a new file is
created. Here your program will start writing content from the
beginning of the file.
ab Opens a text file for writing in appending mode. If it does not exist,
then a new file is created. Here your program will start appending
content in the existing file content.
r+b Opens a text file for both reading and writing.
w+b Opens a text file for both reading and writing. It first truncates the file
to zero length if it exists, otherwise creates a file if it does not exist.
a+b Opens a text file for both reading and writing. It creates the file if it
does not exist. The reading will start from the beginning but writing
can only be appended.
By- Er. Indrajeet Sinha , +919509010997
 Closing file
To close a file, use the fclose( ) function. The prototype of
this function is −
int fclose( FILE *fp );
The fclose(-) function returns zero on success, or EOF if
there is an error in closing the file. This function actually
flushes any data still pending in the buffer to the file,
closes the file, and releases any memory used for the file.
The EOF is a constant defined in the header file stdio.h.
16
By- Er. Indrajeet Sinha , +919509010997
 Writing a File
Following is the simplest function to write individual
characters to a stream −
int fputc( int c, FILE *fp );
The function fputc() writes the character value of the
argument c to the output stream referenced by fp. It
returns the written character written on success
otherwise EOF if there is an error. You can use the
following functions to write a null-terminated string to a
stream −
int fputs( const char *s, FILE *fp );
The function fputs() writes the string s to the output
stream referenced by fp. It returns a non-negative value on
success, otherwise EOF is returned in case of any error.
17
By- Er. Indrajeet Sinha , +919509010997
CONT..
#include <stdio.h>
main()
{
FILE *fp; fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...n");
fputs("This is testing for fputs...n", fp);
fclose(fp);
}
When the above code is compiled and executed, it creates a
new file test.txt in /tmp directory and writes two lines
using two different functions.  18
By- Er. Indrajeet Sinha , +919509010997
CONT..
 Reading a File
Given below is the simplest function to read a single
character from a file −
int fgetc( FILE * fp );
The fgetc() function reads a character from the input file
referenced by fp. The return value is the character read, or
in case of any error, it returns EOF. The following function
allows to read a string from a stream −
char *fgets( char *buf, int n, FILE *fp );
The functions fgets() reads up to n-1 characters from the
input stream referenced by fp. It copies the read string into
the buffer buf, appending a null character to terminate
the string.
19
By- Er. Indrajeet Sinha , +919509010997
CONT..
 If this function encounters a newline character 'n' or the
end of the file EOF before they have read the maximum
number of characters, then it returns only the characters
read up to that point including the new line character. You
can also use int fscanf(FILE *fp, const char
*format, ...) function to read strings from a file, but it stops
reading after encountering the first space character.
20
By- Er. Indrajeet Sinha , +919509010997
CONT..
main()
{
FILE *fp;
char buff[255];
fp = fopen("/tmp/test.txt", "r");
fscanf(fp, "%s", buff); printf("1 : %sn", buff );
fgets(buff, 255, (FILE*)fp); printf("2: %sn", buff );
fgets(buff, 255, (FILE*)fp); printf("3: %sn", buff );
fclose(fp);
}
Note: First, fscanf() read just This because after that, it
encountered a space, second call is for fgets() which reads the
remaining line till it encountered end of line. Finally, the last
call fgets() reads the second line completely.
21
Output:-
1 : This
2: is testing for fprintf...
3: This is testing for fputs...
By- Er. Indrajeet Sinha , +919509010997
3.2.3 BINARY & TEXT FILES
 Definition
 Text File:-A text file is a file that is properly understood
as a sequence of character data, separated into lines. when
a text file is displayed as a sequence of characters, it is
easily human-readable.
 Binary File:- A binary file is anything else. A binary file
will include some data that is not written using a
character-encoding standard some number would be
represented using binary within the file, instead of using
the character representation of its various digits
22
FPRINTF(), FSCANF(),
FEOF()
Lecture no.- 30,
UNIT- III
By- Er. Indrajeet Sinha , +919509010997
3.2.4 FPRINTF(), FSCANF(), FEOF()
 C library function - fprintf()
Description
The C library function int fprintf(FILE *stream, const char
*format, ...) sends formatted output to a stream.
Declaration
int fprintf(FILE *stream, const char *format, ...)
Parameters
 stream − This is the pointer to a FILE object that identifies the
stream.
 format − This is the C string that contains the text to be written
to the stream. It can optionally contain embedded format tags
that are replaced by the values specified in subsequent
additional arguments and formatted as requested. Format tags
prototype is %[flags][width][.precision][length]specifier,
which is explained in next slide. 
24
By- Er. Indrajeet Sinha , +919509010997
specifier Output
c Character
d or i Signed decimal integer
e Scientific notation (mantissa/exponent) using e
character
E Scientific notation (mantissa/exponent) using E
character
f Decimal floating point
g Uses the shorter of %e or %f
G Uses the shorter of %E or %f
25
By- Er. Indrajeet Sinha , +919509010997
CONT..
o Signed octal
s String of characters
u Unsigned decimal integer
x Unsigned hexadecimal integer
X Unsigned hexadecimal integer (capital letters)
p Pointer address
n Nothing printed
% Character
26
By- Er. Indrajeet Sinha , +919509010997
 Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fp; fp = fopen ("file.txt", "w+");
fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
fclose(fp);
return(0);
}
Let us compile and run the above program that will create a
file file.txt with the following content −
We are in 2012
27
By- Er. Indrajeet Sinha , +919509010997
 C library function - fscanf(),
Description
The C library function int fscanf(FILE *stream, const
char *format, ...) reads formatted input from a stream.
Declaration
Following is the declaration for fscanf() function.
int fscanf(FILE *stream, const char *format, ...)
Parameters
stream − This is the pointer to a FILE object that identifies the
stream.
format − This is the C string that contains one or more of the
following items − Whitespace character, Non-whitespace
character and Format specifiers. A format specifier will be
as [=%[*][width][modifiers]type=], which is explained in
next slide.
28
By- Er. Indrajeet Sinha , +919509010997
Argument Description
* This is an optional starting asterisk indicates that the
data is to be read from the stream but ignored, i.e. it is
not stored in the corresponding argument.
width This specifies the maximum number of characters to be
read in the current reading operation.
modifiers Specifies a size different from int (in the case of d, i and
n), unsigned int (in the case of o, u and x) or float (in the
case of e, f and g) for the data pointed by the
corresponding additional argument: h : short int (for d, i
and n), or unsigned short int (for o, u and x) l : long int
(for d, i and n), or unsigned long int (for o, u and x), or
double (for e, f and g) L : long double (for e, f and g)
type A character specifying the type of data to be read and
how it is expected to be read. See next table. 29
By- Er. Indrajeet Sinha , +919509010997
 Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str1[10],
str2[10], str3[10];
int year;
FILE * fp; fp = fopen ("file.txt", "w+");
fputs("We are in 2012", fp);
rewind(fp);
fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
printf("Read String1 |%s|n", str1 );
printf("Read String2 |%s|n", str2 );
printf("Read String3 |%s|n", str3 );
printf("Read Integer |%d|n", year );
fclose(fp);
return(0); }
30
Output:
Read String1 |We|
Read String2 |are|
Read String3 |in|
Read Integer |
2012|
By- Er. Indrajeet Sinha , +919509010997
C LIBRARY FUNCTION - FEOF()
Description
The C library function int feof(FILE *stream) tests the end-
of-file indicator for the given stream.
Declaration
Following is the declaration for feof() function.
int feof(FILE *stream)Parameters
stream − This is the pointer to a FILE object that identifies
the stream.
Return Value
This function returns a non-zero value when End-of-File
indicator associated with the stream is set, else zero is
returned.
31
By- Er. Indrajeet Sinha , +919509010997
EXAMPLE:
#include <stdio.h>
int main ()
{
FILE *fp; int c; fp = fopen("file.txt","r");
if(fp == NULL)
{
perror("Error in opening file");
return(-1);
} while(1)
{ c = fgetc(fp);
if( feof(fp) )
{ break ;
}
printf("%c", c);
} fclose(fp);
return(0);
}
32
NOTE:Assuming we have a text file file.txt,
which has the following content. This file will be
used as an input for our example program −
This is indrajeet
Output:
This is indrajeet
FREAD(), FWRITE()
Lecture no.- 31,
UNIT- III
By- Er. Indrajeet Sinha , +919509010997
3.2.6 FREAD(), FWRITE()
 C library function - fread()
Description
The C library function size_t fread(void *ptr, size_t size,
size_t nmemb, FILE *stream) reads data from the
given stream into the array pointed to, by ptr.
Declaration
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Parameters
ptr − This is the pointer to a block of memory with a minimum size
of size*nmemb bytes.
size − This is the size in bytes of each element to be read.
nmemb − This is the number of elements, each one with a size
of size bytes.
stream − This is the pointer to a FILE object that specifies an input
stream.
34
By- Er. Indrajeet Sinha , +919509010997
 Example
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp; char c[] = "this is Indrajeet";
char buffer[100]; /* Open file for both reading and writing */
fp = fopen("file.txt", "w+"); /* Write data to the file */
fwrite(c, strlen(c) + 1, 1, fp); /* Seek to the beginning of the file */
fseek(fp, SEEK_SET, 0); /* Read and display data */
fread(buffer, strlen(c)+1, 1, fp);
printf("%sn", buffer);
fclose(fp);
return(0);
}
Output:- this is Indrajeet
35
By- Er. Indrajeet Sinha , +919509010997
 C library function - fwrite()
Description
The C library function size_t fwrite(const void *ptr, size_t size,
size_t nmemb, FILE *stream) writes data from the array pointed
to, by ptr to the given stream.
Declaration
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Parameters
ptr − This is the pointer to the array of elements to be written.
size − This is the size in bytes of each element to be written.
nmemb − This is the number of elements, each one with a size
of size bytes.
stream − This is the pointer to a FILE object that specifies an output
stream.
36
By- Er. Indrajeet Sinha , +919509010997
#include<stdio.h>
int main ()
{
FILE *fp; char str[] = "This is Arya First year Department";
fp = fopen( "file.txt" , "w" );
fwrite(str , 1 , sizeof(str) , fp );
fclose(fp);
return(0);
}
Note:- Let us compile and run the above program that will
create a file file.txt which will have following content −
This is Arya First year Department 37
By- Er. Indrajeet Sinha , +919509010997
Now let's see the content of the above file using the following
program
#include <stdio.h>
int main ()
{
FILE *fp; int c; fp = fopen("file.txt","r");
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
38
Output:
This is Arya First year
Department
By- Er. Indrajeet Sinha , +919509010997
3.2.8 SEQUENTIAL VS. RANDOM
ACCESS FILES
 In computer programming, the two main types of file handling
are:
 Sequential;
 Random access.
 Definition:
Sequential files are generally used in cases where the
program processes the data in a sequential fashion – i.e.
counting words in a text file – although in some cases, random
access can be feigned by moving backwards and forwards over a
sequential file.
True random access file handling, however, only accesses the
file at the point at which the data should be read or written,
rather than having to process it sequentially. A hybrid
approach is also possible whereby a part of the file is used for
sequential access to locate something in the random access
portion of the file, in much the same way that a File Allocation
Table (FAT) works.
39
By- Er. Indrajeet Sinha , +919509010997
40
 For Detail Click Here
DYNAMIC MEMORY
ALLOCATION
Lecture no.- 32,
UNIT- III
By- Er. Indrajeet Sinha , +919509010997
3.3.1 DYNAMIC MEMORY
ALLOCATION
 C dynamic memory allocation refers to performing  manual
memory management for dynamic memory allocation in the C
programming language via a group of functions in the C
standard library, namely malloc, realloc, calloc and free.
 3.3.1.1 Malloc
Description
The C library function void *malloc(size_t size) allocates the
requested memory and returns a pointer to it.
Declaration
void *malloc(size_t size)
Parameters
size -- This is the size of the memory block, in bytes.
42
By- Er. Indrajeet Sinha , +919509010997
 Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str; /* Initial memory allocation */
str = (char *)
malloc(15);
strcpy(str, " Indrajeet Sinha ");
printf("String = %s, Address = %un", str, str); /* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %un", str, str);
free(str);
return(0);
}
43
Output:
String = Indrajeet Sinha,
Address = 355090448
String = Indrajeet Sinha,
Address = 355090448
By- Er. Indrajeet Sinha , +919509010997
 Definition:
When calloc is used to allocate a block of memory, the
allocated region is initialized to zeroes.
In contrast, malloc does not
touch the contents of the allocated block of memory, which
means it contains garbage values.
Description
The C library function void *calloc(size_t nitems, size_t
size)allocates the requested memory and returns a pointer to it.
The difference in malloc and calloc is that malloc does not set the
memory to zero where as calloc sets allocated memory to zero.
Declaration
void *calloc(size_t nitems, size_t size)
Parameters
nitems -- This is the number of elements to be allocated.
size -- This is the size of elements.
44
By- Er. Indrajeet Sinha , +919509010997
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n; int *a;
printf("Number of elements to be entered:");
scanf("%d",&n);
a = (int*)calloc(n, sizeof(int));
printf("Enter %d numbers:n",n);
for( i=0 ; i < n ; i++ )
{
scanf("%d",&a[i]);
}
printf("The numbers entered are: ");
for( i=0 ; i < n ; i++ )
{
printf("%d ",a[i]);
} free( a );
return(0);
}
45
Output:
Number of elements to be
entered:3
Enter 3 numbers:
22
55
14
The numbers entered are: 22 55 14
By- Er. Indrajeet Sinha , +919509010997
3.3.1.3 FREE
Description
The C library function void free(void *ptr) deallocates the
memory previously allocated by a call to calloc, malloc, or
realloc.
Declaration
void free(void *ptr)
Parameters
ptr -- This is the pointer to a memory block previously
allocated with malloc, calloc or realloc to be deallocated. If a
null pointer is passed as argument, no action occurs.
Return Value
This function does not return any value.
46
By- Er. Indrajeet Sinha , +919509010997
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str; /* Initial memory allocation */
str = (char *) malloc(15);
strcpy(str, “Indra jeet Sinha");
printf("String = %s, Address = %un", str, str); /* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %un", str, str); /* Deallocate allocated memory */
free(str);
return(0);
}
47
Output:
String = Indrajeet sinha, Address =
355090448 String = Indrajeet sinha,
Address = 355090448
By- Er. Indrajeet Sinha , +919509010997
3.3.1.4 REALLOC
Description
The C library function void *realloc(void *ptr, size_t
size)attempts to resize the memory block pointed to
by ptr that was previously allocated with a call
to malloc or calloc.
Declaration
void *realloc(void *ptr, size_t size)
Parameters
ptr -- This is the pointer to a memory block previously allocated
with malloc, calloc or realloc to be reallocated. If this is
NULL, a new block is allocated and a pointer to it is returned
by the function.
size -- This is the new size for the memory block, in bytes. If it
is 0 and ptr points to an existing block of memory, the
memory block pointed by ptr is deallocated and a NULL
pointer is returned.
48
By- Er. Indrajeet Sinha , +919509010997
Return Value
This function returns a pointer to the newly allocated memory, or
NULL if the request fails.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
char *str; /* Initial memory allocation */
str = (char *)
malloc(15);
strcpy(str, “Indrajeet Sinha");
printf("String = %s, Address = %un", str, str); /* Reallocating memory */
str = (char *)
realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %un", str, str);
free(str); return(0); }
49
Output:
String = Indrajeet sinha, Address =
355090448 String = Indrajeet sinha,
Address = 355090448

More Related Content

PPT
Facade pattern
PPTX
Java - Generic programming
PPT
Introduction to CLIPS Expert System
PPTX
Binary Search Tree in Data Structure
PPTX
Arrays in Data Structure and Algorithm
PPTX
Data file handling in python introduction,opening & closing files
PDF
Algorithms Lecture 7: Graph Algorithms
Facade pattern
Java - Generic programming
Introduction to CLIPS Expert System
Binary Search Tree in Data Structure
Arrays in Data Structure and Algorithm
Data file handling in python introduction,opening & closing files
Algorithms Lecture 7: Graph Algorithms

What's hot (20)

PPTX
PPTX
Java fx
PPTX
Presentation on Elementary data structures
PPT
Loaders complete
PDF
Polymorphism in oop
PPTX
How to download and install Python - lesson 2
PPT
Data Structure and Algorithms Hashing
PDF
PPT
Data structures using c
PPT
Fundamentals of data structures
PPTX
Encapsulation
PPT
PPT
Generics in java
PPTX
Flask – Python
PPTX
Python-Encapsulation.pptx
PPT
JDBC – Java Database Connectivity
PPTX
SOLID - Principles of Object Oriented Design
PPTX
Regular expressions in Python
PDF
Object oriented concepts ppt
PPSX
JDBC: java DataBase connectivity
Java fx
Presentation on Elementary data structures
Loaders complete
Polymorphism in oop
How to download and install Python - lesson 2
Data Structure and Algorithms Hashing
Data structures using c
Fundamentals of data structures
Encapsulation
Generics in java
Flask – Python
Python-Encapsulation.pptx
JDBC – Java Database Connectivity
SOLID - Principles of Object Oriented Design
Regular expressions in Python
Object oriented concepts ppt
JDBC: java DataBase connectivity
Ad

Viewers also liked (13)

PPTX
Pf cs102 programming-8 [file handling] (1)
PPTX
Computer Viruses
PPT
File organization techniques
PPTX
The Internet & Multimedia Integration
PPTX
The Internet and Multimedia
PPTX
File Organization
PPTX
BASIC COMPUTER ARCHITECTURE
PPTX
Basic Computer Architecture
PPT
Dbms models
PPT
File system
PPTX
Ppt on internet
PPT
BASIC CONCEPTS OF COMPUTER NETWORKS
Pf cs102 programming-8 [file handling] (1)
Computer Viruses
File organization techniques
The Internet & Multimedia Integration
The Internet and Multimedia
File Organization
BASIC COMPUTER ARCHITECTURE
Basic Computer Architecture
Dbms models
File system
Ppt on internet
BASIC CONCEPTS OF COMPUTER NETWORKS
Ad

Similar to file handling, dynamic memory allocation (20)

PDF
4 text file
PDF
Handout#01
PPTX
C Programming Unit-5
PDF
Chapter 13.1.10
PDF
ppt5-190810161800 (1).pdf
PPTX
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
PPTX
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
PDF
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
DOCX
Understanding c file handling functions with examples
PPS
C programming session 08
PPTX
pre processor and file handling in c language ppt
DOCX
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
PPT
file_handling_in_c.ppt
PDF
Module 5 file cp
PDF
Lk module4 file
PPTX
MODULE 8-File and preprocessor.pptx for c program learners easy learning
PPT
file handling1
DOCX
Satz1
PPT
file.ppt
DOCX
Unit 5 dwqb ans
4 text file
Handout#01
C Programming Unit-5
Chapter 13.1.10
ppt5-190810161800 (1).pdf
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Understanding c file handling functions with examples
C programming session 08
pre processor and file handling in c language ppt
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
file_handling_in_c.ppt
Module 5 file cp
Lk module4 file
MODULE 8-File and preprocessor.pptx for c program learners easy learning
file handling1
Satz1
file.ppt
Unit 5 dwqb ans

More from indra Kishor (6)

PPT
Control Statements, Array, Pointer, Structures
PPT
Unit v computer, number system
PPT
Programming in c
PPT
Unit iv functions
PPT
Trevel presentation
PPT
Trevel presentation
Control Statements, Array, Pointer, Structures
Unit v computer, number system
Programming in c
Unit iv functions
Trevel presentation
Trevel presentation

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
737-MAX_SRG.pdf student reference guides
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
PPT on Performance Review to get promotions
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Artificial Intelligence
PPT
Mechanical Engineering MATERIALS Selection
DOCX
573137875-Attendance-Management-System-original
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Foundation to blockchain - A guide to Blockchain Tech
737-MAX_SRG.pdf student reference guides
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPT on Performance Review to get promotions
CYBER-CRIMES AND SECURITY A guide to understanding
Artificial Intelligence
Mechanical Engineering MATERIALS Selection
573137875-Attendance-Management-System-original
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
UNIT 4 Total Quality Management .pptx
CH1 Production IntroductoryConcepts.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

file handling, dynamic memory allocation

  • 1. FILE HANDLING, DYNAMIC MEMORY ALLOCATION UNIT- III Fundamental of Computer programming -206
  • 2. By- Er. Indrajeet Sinha , +919509010997 UNIT-III  Array of Structure and pointers  File Handling  Opening & Closing file  fread(), fwrite()  Dynamic Memory Allocation 2
  • 3. ARRAY OF STRUCTURE AND POINTERS Lecture no.- 28, UNIT- III
  • 4. By- Er. Indrajeet Sinha , +919509010997 3.1.1 ARRAY OF STRUCTURES  Definition: Structure is used to store the information of One particular object but if we need to store such 100 objects then Array of Structure is used.  Example : struct Bookinfo { char[20] bname; int pages; int price; } Book[100]; 4 1.Here Book structure is used to Store the information of one Book. 2. In case if we need to store the Information of 100 books then Array of Structure is used. 3. b1[0] stores the Information of 1st Book , b1[1] stores the information of 2nd Book and So on We can store the information of 100 books.
  • 5. By- Er. Indrajeet Sinha , +919509010997 3.1.2 POINTERS TO STRUCTURES  Pointer to Structure in C Programming  Address of Pointer variable can be obtained using ‘&’operator.  Address of such Structure can be assigned to the Pointer variable .  Pointer Variable which stores the address of Structure must be declared as Pointer to Structure .  Pointer to Structure Syntax : struct student_database { char name[10]; int roll; int marks; }stud1; struct student_database *ptr; ptr = &stud1; How to Access Structure Members? 5
  • 6. By- Er. Indrajeet Sinha , +919509010997 Simple program without Pointer variable: #include <stdio.h> int main() { struct student database { char name[10]; int roll; int marks; }stud1; stud1.roll = 10; stud1.marks = 90; printf("Roll Number : %d",stud1.roll); printf("Marks of Student : %d",stud1.marks); return 0; } 6 Output: Roll Number : 10 Marks of Student : 90
  • 7. By- Er. Indrajeet Sinha , +919509010997  Simple program with Pointer variable: #include <stdio.h> int main(int argc, char *argv[]) { struct student_database { char name[10]; int roll; int marks; }stud1 = {"Pritesh",90,90}; struct student_database *ptr; ptr = &stud1; printf("Roll Number : %d",(*ptr).roll); printf("Marks of Student : %d",(*ptr).marks); return 0; } 7 Output: Roll Number : 90 Marks of Student : 90
  • 8. By- Er. Indrajeet Sinha , +919509010997 3.1.3 ARRAY OF POINTERS  Array of pointers 8
  • 10. By- Er. Indrajeet Sinha , +919509010997 3.2.1 INTRODUCTION OF FILE HANDLING  Drawbacks of Traditional I/O System. 1. Until now we are using Console Oriented I/O functions. “Console Application” means an application that has a text- based interface. (black screen window)) 2. Most applications require a large amount of data , if this data is entered through console then it will be quite time consuming task. 3. Main drawback of using Traditional I/O :- data is temporary (and will not be available during re-execution ) 10
  • 11. By- Er. Indrajeet Sinha , +919509010997  File handling in C : 1. New way of dealing with data is file handling. 2. Data is stored onto the disk and can be retrieve whenever require. 3. Output of the program may be stored onto the disk 4. In C we have many functions that deals with file handling 5. A file is a collection of bytes stored on a secondary storagedevice (generally a disk) Collection of byte may be interpreted as –  Single character  Single Word  Single Line  Complete Structure. 11
  • 12. By- Er. Indrajeet Sinha , +919509010997 12
  • 13. By- Er. Indrajeet Sinha , +919509010997 3.2.2 OPENING & CLOSING FILE  Opening Files We can use the fopen( ) function to create a new file or to open an existing file. This call will initialize an object of the type FILE, which contains all the information necessary to control the stream. The prototype of this function call is as follows − FILE *fopen( const char * filename, const char * mode ); Note:- Here, filename is a string literal, which will use to name of our file, and access mode can have any one. 13
  • 14. By- Er. Indrajeet Sinha , +919509010997 Mode Description r Opens an existing text file for reading purpose. w Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file. a Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content. r+ Opens a text file for both reading and writing. w+ Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. a+ Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended. 14
  • 15. By- Er. Indrajeet Sinha , +919509010997 If you are going to handle binary files, then we will use following access modes instead of the above mentioned ones 15 Mode Description rb Opens an existing text file for reading purpose. wb Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file. ab Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content. r+b Opens a text file for both reading and writing. w+b Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist. a+b Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.
  • 16. By- Er. Indrajeet Sinha , +919509010997  Closing file To close a file, use the fclose( ) function. The prototype of this function is − int fclose( FILE *fp ); The fclose(-) function returns zero on success, or EOF if there is an error in closing the file. This function actually flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the file. The EOF is a constant defined in the header file stdio.h. 16
  • 17. By- Er. Indrajeet Sinha , +919509010997  Writing a File Following is the simplest function to write individual characters to a stream − int fputc( int c, FILE *fp ); The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error. You can use the following functions to write a null-terminated string to a stream − int fputs( const char *s, FILE *fp ); The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error. 17
  • 18. By- Er. Indrajeet Sinha , +919509010997 CONT.. #include <stdio.h> main() { FILE *fp; fp = fopen("/tmp/test.txt", "w+"); fprintf(fp, "This is testing for fprintf...n"); fputs("This is testing for fputs...n", fp); fclose(fp); } When the above code is compiled and executed, it creates a new file test.txt in /tmp directory and writes two lines using two different functions.  18
  • 19. By- Er. Indrajeet Sinha , +919509010997 CONT..  Reading a File Given below is the simplest function to read a single character from a file − int fgetc( FILE * fp ); The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOF. The following function allows to read a string from a stream − char *fgets( char *buf, int n, FILE *fp ); The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. 19
  • 20. By- Er. Indrajeet Sinha , +919509010997 CONT..  If this function encounters a newline character 'n' or the end of the file EOF before they have read the maximum number of characters, then it returns only the characters read up to that point including the new line character. You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file, but it stops reading after encountering the first space character. 20
  • 21. By- Er. Indrajeet Sinha , +919509010997 CONT.. main() { FILE *fp; char buff[255]; fp = fopen("/tmp/test.txt", "r"); fscanf(fp, "%s", buff); printf("1 : %sn", buff ); fgets(buff, 255, (FILE*)fp); printf("2: %sn", buff ); fgets(buff, 255, (FILE*)fp); printf("3: %sn", buff ); fclose(fp); } Note: First, fscanf() read just This because after that, it encountered a space, second call is for fgets() which reads the remaining line till it encountered end of line. Finally, the last call fgets() reads the second line completely. 21 Output:- 1 : This 2: is testing for fprintf... 3: This is testing for fputs...
  • 22. By- Er. Indrajeet Sinha , +919509010997 3.2.3 BINARY & TEXT FILES  Definition  Text File:-A text file is a file that is properly understood as a sequence of character data, separated into lines. when a text file is displayed as a sequence of characters, it is easily human-readable.  Binary File:- A binary file is anything else. A binary file will include some data that is not written using a character-encoding standard some number would be represented using binary within the file, instead of using the character representation of its various digits 22
  • 24. By- Er. Indrajeet Sinha , +919509010997 3.2.4 FPRINTF(), FSCANF(), FEOF()  C library function - fprintf() Description The C library function int fprintf(FILE *stream, const char *format, ...) sends formatted output to a stream. Declaration int fprintf(FILE *stream, const char *format, ...) Parameters  stream − This is the pointer to a FILE object that identifies the stream.  format − This is the C string that contains the text to be written to the stream. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. Format tags prototype is %[flags][width][.precision][length]specifier, which is explained in next slide.  24
  • 25. By- Er. Indrajeet Sinha , +919509010997 specifier Output c Character d or i Signed decimal integer e Scientific notation (mantissa/exponent) using e character E Scientific notation (mantissa/exponent) using E character f Decimal floating point g Uses the shorter of %e or %f G Uses the shorter of %E or %f 25
  • 26. By- Er. Indrajeet Sinha , +919509010997 CONT.. o Signed octal s String of characters u Unsigned decimal integer x Unsigned hexadecimal integer X Unsigned hexadecimal integer (capital letters) p Pointer address n Nothing printed % Character 26
  • 27. By- Er. Indrajeet Sinha , +919509010997  Example: #include <stdio.h> #include <stdlib.h> int main() { FILE * fp; fp = fopen ("file.txt", "w+"); fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012); fclose(fp); return(0); } Let us compile and run the above program that will create a file file.txt with the following content − We are in 2012 27
  • 28. By- Er. Indrajeet Sinha , +919509010997  C library function - fscanf(), Description The C library function int fscanf(FILE *stream, const char *format, ...) reads formatted input from a stream. Declaration Following is the declaration for fscanf() function. int fscanf(FILE *stream, const char *format, ...) Parameters stream − This is the pointer to a FILE object that identifies the stream. format − This is the C string that contains one or more of the following items − Whitespace character, Non-whitespace character and Format specifiers. A format specifier will be as [=%[*][width][modifiers]type=], which is explained in next slide. 28
  • 29. By- Er. Indrajeet Sinha , +919509010997 Argument Description * This is an optional starting asterisk indicates that the data is to be read from the stream but ignored, i.e. it is not stored in the corresponding argument. width This specifies the maximum number of characters to be read in the current reading operation. modifiers Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument: h : short int (for d, i and n), or unsigned short int (for o, u and x) l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g) L : long double (for e, f and g) type A character specifying the type of data to be read and how it is expected to be read. See next table. 29
  • 30. By- Er. Indrajeet Sinha , +919509010997  Example #include <stdio.h> #include <stdlib.h> int main() { char str1[10], str2[10], str3[10]; int year; FILE * fp; fp = fopen ("file.txt", "w+"); fputs("We are in 2012", fp); rewind(fp); fscanf(fp, "%s %s %s %d", str1, str2, str3, &year); printf("Read String1 |%s|n", str1 ); printf("Read String2 |%s|n", str2 ); printf("Read String3 |%s|n", str3 ); printf("Read Integer |%d|n", year ); fclose(fp); return(0); } 30 Output: Read String1 |We| Read String2 |are| Read String3 |in| Read Integer | 2012|
  • 31. By- Er. Indrajeet Sinha , +919509010997 C LIBRARY FUNCTION - FEOF() Description The C library function int feof(FILE *stream) tests the end- of-file indicator for the given stream. Declaration Following is the declaration for feof() function. int feof(FILE *stream)Parameters stream − This is the pointer to a FILE object that identifies the stream. Return Value This function returns a non-zero value when End-of-File indicator associated with the stream is set, else zero is returned. 31
  • 32. By- Er. Indrajeet Sinha , +919509010997 EXAMPLE: #include <stdio.h> int main () { FILE *fp; int c; fp = fopen("file.txt","r"); if(fp == NULL) { perror("Error in opening file"); return(-1); } while(1) { c = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", c); } fclose(fp); return(0); } 32 NOTE:Assuming we have a text file file.txt, which has the following content. This file will be used as an input for our example program − This is indrajeet Output: This is indrajeet
  • 34. By- Er. Indrajeet Sinha , +919509010997 3.2.6 FREAD(), FWRITE()  C library function - fread() Description The C library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given stream into the array pointed to, by ptr. Declaration size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) Parameters ptr − This is the pointer to a block of memory with a minimum size of size*nmemb bytes. size − This is the size in bytes of each element to be read. nmemb − This is the number of elements, each one with a size of size bytes. stream − This is the pointer to a FILE object that specifies an input stream. 34
  • 35. By- Er. Indrajeet Sinha , +919509010997  Example #include <stdio.h> #include <string.h> int main() { FILE *fp; char c[] = "this is Indrajeet"; char buffer[100]; /* Open file for both reading and writing */ fp = fopen("file.txt", "w+"); /* Write data to the file */ fwrite(c, strlen(c) + 1, 1, fp); /* Seek to the beginning of the file */ fseek(fp, SEEK_SET, 0); /* Read and display data */ fread(buffer, strlen(c)+1, 1, fp); printf("%sn", buffer); fclose(fp); return(0); } Output:- this is Indrajeet 35
  • 36. By- Er. Indrajeet Sinha , +919509010997  C library function - fwrite() Description The C library function size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream. Declaration size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) Parameters ptr − This is the pointer to the array of elements to be written. size − This is the size in bytes of each element to be written. nmemb − This is the number of elements, each one with a size of size bytes. stream − This is the pointer to a FILE object that specifies an output stream. 36
  • 37. By- Er. Indrajeet Sinha , +919509010997 #include<stdio.h> int main () { FILE *fp; char str[] = "This is Arya First year Department"; fp = fopen( "file.txt" , "w" ); fwrite(str , 1 , sizeof(str) , fp ); fclose(fp); return(0); } Note:- Let us compile and run the above program that will create a file file.txt which will have following content − This is Arya First year Department 37
  • 38. By- Er. Indrajeet Sinha , +919509010997 Now let's see the content of the above file using the following program #include <stdio.h> int main () { FILE *fp; int c; fp = fopen("file.txt","r"); while(1) { c = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", c); } fclose(fp); return(0); 38 Output: This is Arya First year Department
  • 39. By- Er. Indrajeet Sinha , +919509010997 3.2.8 SEQUENTIAL VS. RANDOM ACCESS FILES  In computer programming, the two main types of file handling are:  Sequential;  Random access.  Definition: Sequential files are generally used in cases where the program processes the data in a sequential fashion – i.e. counting words in a text file – although in some cases, random access can be feigned by moving backwards and forwards over a sequential file. True random access file handling, however, only accesses the file at the point at which the data should be read or written, rather than having to process it sequentially. A hybrid approach is also possible whereby a part of the file is used for sequential access to locate something in the random access portion of the file, in much the same way that a File Allocation Table (FAT) works. 39
  • 40. By- Er. Indrajeet Sinha , +919509010997 40  For Detail Click Here
  • 42. By- Er. Indrajeet Sinha , +919509010997 3.3.1 DYNAMIC MEMORY ALLOCATION  C dynamic memory allocation refers to performing  manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free.  3.3.1.1 Malloc Description The C library function void *malloc(size_t size) allocates the requested memory and returns a pointer to it. Declaration void *malloc(size_t size) Parameters size -- This is the size of the memory block, in bytes. 42
  • 43. By- Er. Indrajeet Sinha , +919509010997  Example #include <stdio.h> #include <stdlib.h> int main() { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, " Indrajeet Sinha "); printf("String = %s, Address = %un", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %un", str, str); free(str); return(0); } 43 Output: String = Indrajeet Sinha, Address = 355090448 String = Indrajeet Sinha, Address = 355090448
  • 44. By- Er. Indrajeet Sinha , +919509010997  Definition: When calloc is used to allocate a block of memory, the allocated region is initialized to zeroes. In contrast, malloc does not touch the contents of the allocated block of memory, which means it contains garbage values. Description The C library function void *calloc(size_t nitems, size_t size)allocates the requested memory and returns a pointer to it. The difference in malloc and calloc is that malloc does not set the memory to zero where as calloc sets allocated memory to zero. Declaration void *calloc(size_t nitems, size_t size) Parameters nitems -- This is the number of elements to be allocated. size -- This is the size of elements. 44
  • 45. By- Er. Indrajeet Sinha , +919509010997 #include <stdio.h> #include <stdlib.h> int main() { int i, n; int *a; printf("Number of elements to be entered:"); scanf("%d",&n); a = (int*)calloc(n, sizeof(int)); printf("Enter %d numbers:n",n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf("The numbers entered are: "); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } free( a ); return(0); } 45 Output: Number of elements to be entered:3 Enter 3 numbers: 22 55 14 The numbers entered are: 22 55 14
  • 46. By- Er. Indrajeet Sinha , +919509010997 3.3.1.3 FREE Description The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc. Declaration void free(void *ptr) Parameters ptr -- This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated. If a null pointer is passed as argument, no action occurs. Return Value This function does not return any value. 46
  • 47. By- Er. Indrajeet Sinha , +919509010997 Example #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, “Indra jeet Sinha"); printf("String = %s, Address = %un", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %un", str, str); /* Deallocate allocated memory */ free(str); return(0); } 47 Output: String = Indrajeet sinha, Address = 355090448 String = Indrajeet sinha, Address = 355090448
  • 48. By- Er. Indrajeet Sinha , +919509010997 3.3.1.4 REALLOC Description The C library function void *realloc(void *ptr, size_t size)attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc. Declaration void *realloc(void *ptr, size_t size) Parameters ptr -- This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated. If this is NULL, a new block is allocated and a pointer to it is returned by the function. size -- This is the new size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned. 48
  • 49. By- Er. Indrajeet Sinha , +919509010997 Return Value This function returns a pointer to the newly allocated memory, or NULL if the request fails. Example #include <stdio.h> #include <stdlib.h> int main() { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, “Indrajeet Sinha"); printf("String = %s, Address = %un", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %un", str, str); free(str); return(0); } 49 Output: String = Indrajeet sinha, Address = 355090448 String = Indrajeet sinha, Address = 355090448