C (Dynamic Memory Allocation and File Handling)
C (Dynamic Memory Allocation and File Handling)
By default malloc() returns the pointer of type void, which means we can
assign it any type of pointer
The syntax of this function is as follows:
This is a CAST
(remember them) we want sizeof(char) returns how
that forces the variable n chars much memory a char
to the right type (not takes
needed)
e.g.
#include<stdio.h> Output
int *ip;
ip = (int *) calloc(100, sizeof(int));
It will assign 100 integer elements that are initially
zero
Another minor difference between malloc() and calloc() is that the memory
allocated by the malloc() contains garbage values while the memory
allocated by calloc() functions contains all zeros
free()
The free() function is used to de-allocate the
2.If the allocated memory is much more that what is required by the
current application. In other words, it provides even more precise
and efficient utilization of memory
FILE *fptr;
fptr= fopen ("myfile.dat","r");
/* Read from file */
fclose ("myfile.dat");
/* Ooops - that's wrong */
File opening modes
"w" Searches file. If the file exists, its contents are overwritten. If the
file doesn’t exist, a new file is created. Returns NULL, if unable to open
file.
Operations possible – writing to the file.
"w+" Searches file. If the file exists, its contents are overwritten. If the
file doesn’t exist a new file is created. Returns NULL, if unable to open
file.
Operations possible - writing new contents, reading them back and
modifying existing contents of the file.
FILE *fptr;
fptr= fopen ("file.dat","w");
/* Check it's open */
fprintf (fptr,"Hello World!\n");
#include "stdio.h"
main( ) if ( fp == NULL )
{
fread() causes the data read from the file to be placed in structure
variable. Format is same as that of fwrite().
fwrite() reads the record where the file pointer is currently placed.
There is a provision to Add, Modify, List and Delete records, the operations that
are imperative in any database management.
− Listing records means displaying the existing records on the screen. Naturally,
records should be listed from first record to last record.
− While modifying records, first we must ask the user which record he intends to
modify. Instead of asking the record number to be modified, it would be more
meaningful to ask for the name of the employee whose record is to be modified. On
modifying the record, the existing record gets overwritten by the new record.
− In deleting records, except for the record to be deleted, rest of the records must
first be written to a temporary file, then the original file must be deleted, and the
temporary file must be renamed back to original.
Some points to remember:
• On opening a file a pointer is set up which points to the first record in the
file.
• fread( ) always reads that record where the pointer is currently placed.
Similarly, fwrite( ) always writes the record where the pointer is currently
placed.
• The rewind( ) function places the pointer to the beginning of the file,
irrespective of where it is present right now.
• The fseek( ) function lets us move the pointer from one record to another.
• To move the pointer to the previous record from its current position, we use the
function,
fseek ( fp, -recsize, SEEK_CUR ) ;
• The following fseek( ) would place the pointer beyond the last record in the file.
fseek ( fp, 0, SEEK_END ) ;
•-recsize or 0 are just the offsets that tell the compiler by how many bytes should
the pointer be moved from a particular position.