SlideShare a Scribd company logo
FILE MANAGEMENT
Jigar Jobanputra
• As we know that printf and scanf are console oriented functions,
which always use the terminal like keyboard as the target place.
These functions works fine as well as small application in concern.
But in some real life problems or large problems these functions
are not suitable as it is very cumbersome and time consuming.
• To solve these problems we need flexible approach where we can
store – read - write –update the data onto disks whenever
necessary.
• Language C supports many operation where we can store the data
onto the disks using the concept called file handling or file
management.
• C supports the following operation like :
– Naming a file
– Opening a file
– Reading the data from a file
– Write a data to the file
– Closing a file
• When working with a file steps it to establish
a buffer area, where information is
temporarily stored while being transferred
between the computer’s memory and the
data file. This buffer area allows information
to be read from or written to the data file
more rapidly then would otherwise be
possible. The buffer area is established by
writing
• FILE *pt;
• Where FILE (uppercase required) is a special
data structure type that establishes the buffer
area and pt is a pointer variable that indicates
the beginning of the buffer area. The
structure type file is defined with a system
include file : typically stdio.h
Function name Operation
fopen() Creates new file / Opens a new file
fclose() Closes a file which hass been opened for use
getc() Reads a character form a file
putc() Writes a character to a file
fprintf() Writes a set of data values to a file
fscanf() Reads set of data values from a file
getw() Reads integer data value from a file
putw() Writes integer data value to a file
fseek() Sets a position to a desired position in the file
ftell() Gives the current position in the file
rewind() Sets the position at the beginning of the file
• 1. fopen () :- Creates new file / Opens a new file
•
• If we want to store the data file in the secondary memory , we must the filename
, data structure and purpose.
•
• Filename always contain some extension for example srk.txt or srkins.doc , data
structure means using which library we are storing the content of the file and
purpose means for what operation we want to open the file like for reading
/writing etc..
•
• Syntax:
• FILE *pt; // declares the variable pt as a pointer to the data type FILE
• pt = fopen( “ file name”, ”mode”) ; //
•
• Here , filename is name of the file which you want to open or you want to create
if file does not exists.
• mode specifying the purpose of opening the file.
• Mode can be one of the following :
• r open the file for reading only
• w open the file for reading only
• a open the file for appending
• r+ open the existing file to the beginning both
for reading and writing
• w+ same as w but both for reading and writing
• a+ same as a but both for reading and writing
• Example:
•
• FILE *pt1,*pt2;
• pt1=fopen(“srk.txt”,”w” ); // opens a file srk.txt if it exist or else create it
• pt2=fopen(“srk.doc”,”r” ); // opens a file srk.doc if it exist or else none
•
•
• The fopen function returns a pointer to the beginning of the buffer area
associated with the file. A NULL value is returned if the file cannot be opened as
for example, when and existing file cannot be found. Finally a data file must be
closed at eh end of the program. This ensures that all outstanding information
associated with the file is flushed out from the buffers and all links to the file are
broken. It also prevents any accidental misuse of the file. In case there is a limit to
the number of files that can be kept open simultaneously, closing of unwanted file
might help open the required files. Another instance, where we have to close a file
is when we want to reopen the same file in a different mode. This can be
accomplished with the library function fclose. The syntax is simply:
• 2. fclose() :
•
• Syntax:
•
• fclose(file pointer);
•
•
•
• Example:
•
• #include <stdio.h> main()
• {
• FILE *fp;
• fp=fopen(“SRKINS.txt”,”w”);
• fclose(fp);
• }
• 3. The getc and putc Functions:
•
• The simplest file i/o functions are getc and putc. These are used to handle one character at a time.
Assume that a file is opened with mode w and file pointer fp. Then, the statement
•
• putc(c,fp);
•
• Writes the character contained in the character variable c to the file associated with FILE pointer
fp.
•
• Similarly getc is used to read character from a file that has been opened in read mode. For
example, the statement
•
• c = getc(fp);
•
•
•
• Would reads character from file whose file
pointer is fp. the file pointer moves by one
character position for every operation of getc
and putc. The getc will have been reached.
Therefore, the reading should be terminated
when EOF( End Of File ) is encountered.
•  Write a program to read data from the
keyboard, write it to a file called SRK again read
the same data from the SRK file and display on
the screen.
•  Write a program to read data from the
keyboard, write it to a file called SRK again
read the same data from the SRK file and
display on the screen.
• The getw and putw Functions:
•
• The getw and putw are integer oriented functions. These functions are
useful when we deal with the integer type data value.
•
• getw() is used to
• read integer data value from a file
• putw() is used to
• writes integer data value to a file
•
• Syntax:
• putw(integer,fp); // fp is the pointer variable and integer is the name
of the variable type int
• getw(fp);
• THE FSCANF AND FPRINTF FUNCTIONS :
•
• These functions are used to read or write in formatted from from/to the
files.
•
• Syntax of fprintf ():
• fscanf(fp, “control string”, list);
•
• Example:
• FILE *f1;
• f1=fopen(“input.txt” , “w ” );
• fprintf(f1, “%s %d %f ”, name, age, 7.5); // prints
name,age and 7.5 from the file input
•
• Syntax of fscanf ():
•
• fscanf(fp, “control string”, list);
•
• Example:
•
• FILE *f1;
• f1=fopen(“input.txt” , “w ” );
• fscanf(f1, “%s %d %f ”, name, &age,&per);// writes name,age and
percentage from the file input
• fprintf(f1, “%s %d %f ”, name, age, 7.5);
•
• fcanf returns the number of items that are successfully read. When the
end of the file is reached, it returns the value of EOF.
• 5. The fseek function:
•
• fseek function is used to move the position to a desired location
within the file. It takes the following form.
•
• Syntax:
• feek(fp, offset, position)
•
• fp is a pointer to file concerned. offset is a number of variable of
type long and position is an integer number. The offset specifies
the number of bytes(position) to be moved from the location
specified by position. The position can take one of the following
three values:
• 0 beginning of file
• 1 current position
• 2 end of file
•
• The offset may be positive, meaning move
forward and negative, meaning move backwards.
When the operation is successful, fseek returns a
zero. If we attempt to move beyond the file
boundaries and error occurs and fseek returns.
•  A file named DATA contains a series of
integer numbers. Write a program to read
these numbers and then write all “ODD”
numbers to a file called ODD and all “even”
numbers to a file called EVEN.
DYNAMIC MEMORY ALLOCATION
• The process of allocating memory at run time
is known as dynamic memory allocation.
malloc() and calloc() are library routines
known as that can be used for allocating and
freeing memory during program execution.
• There are four “Memory Management
Functions” available in C library routine for
allocating and freeing a memory during a
program execution.
• malloc() : alloates the memory to a single
element and move the pointer to the first
byte
• calloc() : alloates the memory to an array of
elements
• free() : Frees the previously allocated space
• realloc() : modifies the previously allocated
space
• 1. malloc()
•
• A block of memory may be allocated using the
function malloc(). The function reserves a
block of memory of specified size and returns
a pointer of type void. This means that we can
assign it to any type of pointer. it takes the
following form.
• ptr = ( cast_type * ) malloc( byte_size );
•
• ptr is a pointer of type cast_type. The malloc
returns a pointer of cast_type to an area of
memory of size byte_size. For example,
• X = ( int * ) malloc( 100 * sizeof (int) );
•
• On successful execution of this statement a
memory space equivalent to “100 times for
the size of int” bytes is reserved and the
address of the first byte of the memory
allocated is assigned to the pointer x of type
int. Similarly statement
• cptr = (char *) malloc(10);
•
• Allocates 10 byte of space for the cptr of type
char. We may also use malloc to allocate
space for complex data types such as
structures.
• For example,
• stptr = ( struct store* )malloc( sizeof( struct
store ) );
• Where stptr is pointer of type struct store.
The malloc allocates a block of contiguous
bytes. The allocation can fall if the space in
the heap is not sufficient to satisfy the
request. If it fails it returns a NULL.
2. Calloc()
• Calloc is memory allocation function that is
normally used for requesting memory space
at runtime for storing derived data types such
as arrays and structures. Calloc allocates
multiple block of storage, each of the same
size and then sets all bytes to zero. The
general form of calloc is ;
•
• ptr = (cast_type *) calloc(n,elem_size);
• The above statement allocates contiguous
space of n blocks. Each of size elem_size. All
bytes are initialized to zero and a pointer to
the first byte of allocated region is returned. If
there is not enough space, a NULL pointer is
returned. The following segment of program
allocates space to a structure variable.
• struct student
• {
• char name[25]; float age;
• long int id_num;
• };
• typedef struct student record;
• record *stptr;
• int class size=30;
• stptr = (record *) calloc(class_size,
sizeof(record));
3. free()
• This function is used to allocate the previously
allocated space.
• Look the following form :
•
• free(ptr);
• Ptr is the pointer to the previously allocated
memory space.
• 4. realloc() :
• Sometimes the previously allocated memory
is not sufficient and we need additional space
for more elements. realloc function is used in
such type of situations.
• Syntax:
• ptr=realloc(ptr,new size);
• This function allocates a new memory space
of size new size to the pointer variable ptr and
returns a pointer to the first byte of the
memory block.
•  Write a program to store a character string
in a block of memory space created by malloc
and then modify the same to store a larger
string.

More Related Content

PPTX
File in C language
PPT
PPTX
File handling in c
DOCX
Understanding c file handling functions with examples
PPTX
File handling in C
PPTX
UNIT 10. Files and file handling in C
PPTX
File Management in C
File in C language
File handling in c
Understanding c file handling functions with examples
File handling in C
UNIT 10. Files and file handling in C
File Management in C

What's hot (20)

PPT
File in C Programming
PPTX
C Programming Unit-5
PPT
File handling in 'C'
PPTX
File handling in C by Faixan
PPT
Mesics lecture files in 'c'
PPT
File in c
PPT
File handling in c
PPT
File handling(some slides only)
PPT
File Management
PPT
File handling in c
PDF
Module 03 File Handling in C
PPT
File handling in c
PDF
FILES IN C
PPT
Unit5 C
PPTX
file management in c language
PPTX
File Handling in C
PPTX
File handling in C
PPTX
COM1407: File Processing
PPT
file
PPT
File handling
File in C Programming
C Programming Unit-5
File handling in 'C'
File handling in C by Faixan
Mesics lecture files in 'c'
File in c
File handling in c
File handling(some slides only)
File Management
File handling in c
Module 03 File Handling in C
File handling in c
FILES IN C
Unit5 C
file management in c language
File Handling in C
File handling in C
COM1407: File Processing
file
File handling
Ad

Viewers also liked (6)

PPTX
Java session14
PPTX
C programming
PPT
PPT
File Management Presentation
PPT
File Management
PPT
Making appointment
Java session14
C programming
File Management Presentation
File Management
Making appointment
Ad

Similar to File mangement (20)

PPTX
File management
PDF
637225560972186380.pdf
PPT
Memory allocation in c
PPTX
MODULE 8-File and preprocessor.pptx for c program learners easy learning
PPTX
File management
PPTX
Linux System Programming - Buffered I/O
PPT
How to do file-handling - in C language
PDF
file_c.pdf
PPTX
files c programming handling in computer programming
PPTX
File Handling in C
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPTX
Programming in C Session 4
PDF
Unit 5 File handling in C programming.pdf
PPT
Unit5
PPTX
file handling in c programming with file functions
PPTX
Programming C- File Handling , File Operation
PPTX
File handling With Solve Programs
PPTX
File Management in C
PPT
file_handling_in_c.ppt
File management
637225560972186380.pdf
Memory allocation in c
MODULE 8-File and preprocessor.pptx for c program learners easy learning
File management
Linux System Programming - Buffered I/O
How to do file-handling - in C language
file_c.pdf
files c programming handling in computer programming
File Handling in C
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
Programming in C Session 4
Unit 5 File handling in C programming.pdf
Unit5
file handling in c programming with file functions
Programming C- File Handling , File Operation
File handling With Solve Programs
File Management in C
file_handling_in_c.ppt

More from Jigarthacker (11)

PPTX
Java session13
PPSX
Java session5
PPSX
Java session4
PPSX
Java session3
PPTX
Java session2
PPSX
Computer networks
PPSX
Unit 2
PPSX
Unit 1(sem-iv)
PPSX
Unit 2
PPSX
Unit 1
PDF
Basic object oriented approach
Java session13
Java session5
Java session4
Java session3
Java session2
Computer networks
Unit 2
Unit 1(sem-iv)
Unit 2
Unit 1
Basic object oriented approach

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Pre independence Education in Inndia.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
01-Introduction-to-Information-Management.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Institutional Correction lecture only . . .
Microbial diseases, their pathogenesis and prophylaxis
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pre independence Education in Inndia.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Final Presentation General Medicine 03-08-2024.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Renaissance Architecture: A Journey from Faith to Humanism
01-Introduction-to-Information-Management.pdf
O5-L3 Freight Transport Ops (International) V1.pdf

File mangement

  • 2. • As we know that printf and scanf are console oriented functions, which always use the terminal like keyboard as the target place. These functions works fine as well as small application in concern. But in some real life problems or large problems these functions are not suitable as it is very cumbersome and time consuming. • To solve these problems we need flexible approach where we can store – read - write –update the data onto disks whenever necessary. • Language C supports many operation where we can store the data onto the disks using the concept called file handling or file management.
  • 3. • C supports the following operation like : – Naming a file – Opening a file – Reading the data from a file – Write a data to the file – Closing a file
  • 4. • When working with a file steps it to establish a buffer area, where information is temporarily stored while being transferred between the computer’s memory and the data file. This buffer area allows information to be read from or written to the data file more rapidly then would otherwise be possible. The buffer area is established by writing
  • 5. • FILE *pt; • Where FILE (uppercase required) is a special data structure type that establishes the buffer area and pt is a pointer variable that indicates the beginning of the buffer area. The structure type file is defined with a system include file : typically stdio.h
  • 6. Function name Operation fopen() Creates new file / Opens a new file fclose() Closes a file which hass been opened for use getc() Reads a character form a file putc() Writes a character to a file fprintf() Writes a set of data values to a file fscanf() Reads set of data values from a file getw() Reads integer data value from a file putw() Writes integer data value to a file fseek() Sets a position to a desired position in the file ftell() Gives the current position in the file rewind() Sets the position at the beginning of the file
  • 7. • 1. fopen () :- Creates new file / Opens a new file • • If we want to store the data file in the secondary memory , we must the filename , data structure and purpose. • • Filename always contain some extension for example srk.txt or srkins.doc , data structure means using which library we are storing the content of the file and purpose means for what operation we want to open the file like for reading /writing etc.. • • Syntax: • FILE *pt; // declares the variable pt as a pointer to the data type FILE • pt = fopen( “ file name”, ”mode”) ; // • • Here , filename is name of the file which you want to open or you want to create if file does not exists. • mode specifying the purpose of opening the file.
  • 8. • Mode can be one of the following : • r open the file for reading only • w open the file for reading only • a open the file for appending • r+ open the existing file to the beginning both for reading and writing • w+ same as w but both for reading and writing • a+ same as a but both for reading and writing
  • 9. • Example: • • FILE *pt1,*pt2; • pt1=fopen(“srk.txt”,”w” ); // opens a file srk.txt if it exist or else create it • pt2=fopen(“srk.doc”,”r” ); // opens a file srk.doc if it exist or else none • • • The fopen function returns a pointer to the beginning of the buffer area associated with the file. A NULL value is returned if the file cannot be opened as for example, when and existing file cannot be found. Finally a data file must be closed at eh end of the program. This ensures that all outstanding information associated with the file is flushed out from the buffers and all links to the file are broken. It also prevents any accidental misuse of the file. In case there is a limit to the number of files that can be kept open simultaneously, closing of unwanted file might help open the required files. Another instance, where we have to close a file is when we want to reopen the same file in a different mode. This can be accomplished with the library function fclose. The syntax is simply:
  • 10. • 2. fclose() : • • Syntax: • • fclose(file pointer); • • • • Example: • • #include <stdio.h> main() • { • FILE *fp; • fp=fopen(“SRKINS.txt”,”w”); • fclose(fp); • }
  • 11. • 3. The getc and putc Functions: • • The simplest file i/o functions are getc and putc. These are used to handle one character at a time. Assume that a file is opened with mode w and file pointer fp. Then, the statement • • putc(c,fp); • • Writes the character contained in the character variable c to the file associated with FILE pointer fp. • • Similarly getc is used to read character from a file that has been opened in read mode. For example, the statement • • c = getc(fp); • • •
  • 12. • Would reads character from file whose file pointer is fp. the file pointer moves by one character position for every operation of getc and putc. The getc will have been reached. Therefore, the reading should be terminated when EOF( End Of File ) is encountered. •  Write a program to read data from the keyboard, write it to a file called SRK again read the same data from the SRK file and display on the screen.
  • 13. •  Write a program to read data from the keyboard, write it to a file called SRK again read the same data from the SRK file and display on the screen.
  • 14. • The getw and putw Functions: • • The getw and putw are integer oriented functions. These functions are useful when we deal with the integer type data value. • • getw() is used to • read integer data value from a file • putw() is used to • writes integer data value to a file • • Syntax: • putw(integer,fp); // fp is the pointer variable and integer is the name of the variable type int • getw(fp);
  • 15. • THE FSCANF AND FPRINTF FUNCTIONS : • • These functions are used to read or write in formatted from from/to the files. • • Syntax of fprintf (): • fscanf(fp, “control string”, list); • • Example: • FILE *f1; • f1=fopen(“input.txt” , “w ” ); • fprintf(f1, “%s %d %f ”, name, age, 7.5); // prints name,age and 7.5 from the file input •
  • 16. • Syntax of fscanf (): • • fscanf(fp, “control string”, list); • • Example: • • FILE *f1; • f1=fopen(“input.txt” , “w ” ); • fscanf(f1, “%s %d %f ”, name, &age,&per);// writes name,age and percentage from the file input • fprintf(f1, “%s %d %f ”, name, age, 7.5); • • fcanf returns the number of items that are successfully read. When the end of the file is reached, it returns the value of EOF.
  • 17. • 5. The fseek function: • • fseek function is used to move the position to a desired location within the file. It takes the following form. • • Syntax: • feek(fp, offset, position) • • fp is a pointer to file concerned. offset is a number of variable of type long and position is an integer number. The offset specifies the number of bytes(position) to be moved from the location specified by position. The position can take one of the following three values:
  • 18. • 0 beginning of file • 1 current position • 2 end of file • • The offset may be positive, meaning move forward and negative, meaning move backwards. When the operation is successful, fseek returns a zero. If we attempt to move beyond the file boundaries and error occurs and fseek returns.
  • 19. •  A file named DATA contains a series of integer numbers. Write a program to read these numbers and then write all “ODD” numbers to a file called ODD and all “even” numbers to a file called EVEN.
  • 20. DYNAMIC MEMORY ALLOCATION • The process of allocating memory at run time is known as dynamic memory allocation. malloc() and calloc() are library routines known as that can be used for allocating and freeing memory during program execution. • There are four “Memory Management Functions” available in C library routine for allocating and freeing a memory during a program execution.
  • 21. • malloc() : alloates the memory to a single element and move the pointer to the first byte • calloc() : alloates the memory to an array of elements • free() : Frees the previously allocated space • realloc() : modifies the previously allocated space
  • 22. • 1. malloc() • • A block of memory may be allocated using the function malloc(). The function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. it takes the following form.
  • 23. • ptr = ( cast_type * ) malloc( byte_size ); • • ptr is a pointer of type cast_type. The malloc returns a pointer of cast_type to an area of memory of size byte_size. For example,
  • 24. • X = ( int * ) malloc( 100 * sizeof (int) ); • • On successful execution of this statement a memory space equivalent to “100 times for the size of int” bytes is reserved and the address of the first byte of the memory allocated is assigned to the pointer x of type int. Similarly statement
  • 25. • cptr = (char *) malloc(10); • • Allocates 10 byte of space for the cptr of type char. We may also use malloc to allocate space for complex data types such as structures.
  • 26. • For example, • stptr = ( struct store* )malloc( sizeof( struct store ) ); • Where stptr is pointer of type struct store. The malloc allocates a block of contiguous bytes. The allocation can fall if the space in the heap is not sufficient to satisfy the request. If it fails it returns a NULL.
  • 27. 2. Calloc() • Calloc is memory allocation function that is normally used for requesting memory space at runtime for storing derived data types such as arrays and structures. Calloc allocates multiple block of storage, each of the same size and then sets all bytes to zero. The general form of calloc is ; • • ptr = (cast_type *) calloc(n,elem_size);
  • 28. • The above statement allocates contiguous space of n blocks. Each of size elem_size. All bytes are initialized to zero and a pointer to the first byte of allocated region is returned. If there is not enough space, a NULL pointer is returned. The following segment of program allocates space to a structure variable.
  • 29. • struct student • { • char name[25]; float age; • long int id_num; • }; • typedef struct student record; • record *stptr; • int class size=30; • stptr = (record *) calloc(class_size, sizeof(record));
  • 30. 3. free() • This function is used to allocate the previously allocated space. • Look the following form : • • free(ptr); • Ptr is the pointer to the previously allocated memory space.
  • 31. • 4. realloc() : • Sometimes the previously allocated memory is not sufficient and we need additional space for more elements. realloc function is used in such type of situations. • Syntax: • ptr=realloc(ptr,new size);
  • 32. • This function allocates a new memory space of size new size to the pointer variable ptr and returns a pointer to the first byte of the memory block. •  Write a program to store a character string in a block of memory space created by malloc and then modify the same to store a larger string.