Unit - V Pointers and File Handling

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

UNIT – V

POINTERS and FILE HANDLING

Pointers:
Introduction to Pointers – declaring Pointer Variables – Passing Arguments to
Functions using Pointer – Pointer and Arrays – Dynamic Memory Allocation
File Handling:
Introduction to Files, File modes, File operations, Reading Data from Files, Writing
Data from Files, Detecting the End-of-file.

Introduction:
Computer’s use their memory for storing the instructions of a program as well as the
values of variables that are associated with it. The computer’s memory is a sequential
collection of storage cells. Each cell commonly known as a byte, has a number called address
associated with it. The addresses are numbered starting from zero. The last address depends
on memory size.
Whenever we declare a variable, the system allocates somewhere in a memory an
appropriate location to hold the value of the variable. Since every byte has a unique address
number, this location will have its known address number.

Memory Organization
Consider the following statement, i𝑛𝑡 𝑎 = 179; this statement instructs the system to
find a location for the integer variable ‘a’ and puts the value 179 in that location.

We may have to access to the value 179 by using either the name ‘a’ or address ‘5000’.
Since, memory addresses are simple numbers; they can be assigned to some variables which
can be stored in memory like any other variable. Such variables that hold memory addresses
are called pointers.
Pointer:
A pointer is a variable that contains an address which is a location of another variable
in memory. A pointer enables us to access a variable that is defined outside the function.
Pointers reduce the length and complexity of a program. They increase the execution speed.
1|Page
Understanding a variable
A variable contains a name, value and address of memory location where it stores its
value.
Consider the following variable declaration and memory picture.
i𝑛𝑡 i = 50;
The following would be the memory picture after the memory is allocated to variable ‘i’.

So it is understood that integer variable ‘i’ is allocated two bytes in memory. First
byte is at address 200 and second byte at 201. The value stored in the variable is 50.
Note: Every memory location contains an address. The address of the variable is always the
address of the first byte of the space allocated to a variable.

Pointer Variable:
A pointer variable or a pointer is similar to a variable but where a variable stores data;
a pointer variable stores address of a memory location. So a pointer does contain all
attributes of a variable but what is stored in pointer is interpreted as an address of a memory
location and not as data.

Declaring a Pointer Variable:


A pointer variable (or pointer) is declared same as a variable but contains a * before
the name.
Syntax:
𝑑𝑎𝑡𝑎𝑡𝑦𝑝𝑒 ∗ 𝑝𝑜i𝑛𝑡𝑒𝑟_𝑛𝑎𝑚𝑒;
This tells the compiler three things about the variable ptr_name:
• The ∗ tells that the variable ptr_name is a pointer variable.
• The ptr_name needs a memory location.
• ptr_name points to a variable of a data type.
The following is an example of a pointer variable.
int *p;
• Declares the variable p as a pointer variable that points to an integer data type.
• Remember that the type int refers to the data type of the variable being pointed to by
p and not the type of the value of the pointer.
• Just like a normal variable even a pointer variable contains memory location.

Similarly the statement, float *x; declares x as a pointer to a floating point variable.
Note: A pointer always occupies 2 bytes in memory. This is because to store an address we
need only 2 bytes.

2|Page
Storing address into pointer:
➢ A pointer is used to contain an address, to store the address of variable into pointer
using address operator (&).
➢ The operator & immediately preceding a variable returns the address of the variable
associated with it.
➢ When a pointer contains the address of a variable, then the pointer is said to be
pointing to that variable.
For example, in the following code, pointer p is pointing to variable i.
int i = 50, *p;
p = &i;
The memory image for the above assignment is shown in the figure below:

When p contains the address of i then it is read as p is pointing to i .

Accessing a variable through its pointer:


➢ To access the value of the variable using pointer (that contains address of a variable),
this is done by using another unary operator * (asterisk), usually known as the
indirection operator.
➢ When a pointer points to a memory location we can access the value of that memory
location using a special operator called indirection operator (*).
➢ When a pointer variable is used with indirection operator then you get the value of
the location to which pointer points.

➢ So *p will return 50 because p is pointing to memory location 200 and that is the
location of variable i, where value 50 is stored.

Example:
/*Program to Illustrate the Concept of Pointers*/
#include <stdio.h>
#include<conio.h>
void main()
{
int a = 10;
int *p;
p = &a;
clrscr();
3|Page
printf("\nAddress of a: %u", &a);
printf("\nAddress of a: %u", p);
printf("\nAddress of p: %u", &p);
printf("\nValue of p: %d", p);
printf("\nValue of a: %d", a);
printf("\nValue of a: %d", *(&a));
printf("\nValue of a: %d", *p);
getch();
}
Output:
Address of a: 65494
Address of a: 65494
Address of p: 65496
Value of p: 65494
Value of a: 10
Value of a: 10
Value of a: 10
Example:
/*Program to Illustrate the use of Indirection operator*/
#include <stdio.h>
#include<conio.h>
void main()
{
int x,y;
int *p;
clrscr();
x = 10;
p = &x;
y = *p;
printf("\nValue of x: %d", x);
printf("\n%d is stored at address %u", x,&x);
printf("\n%d is stored at address %u", *p,p);
printf("\n%d is stored at address %u", y,&*p);
printf("\n%u is stored at address %u", p,&p);
printf("\n%d is stored at address %u", y,&y);
*p = 25;
printf("\nValue of x: %d", x);
getch();
}
Output:
Value of x: 10
10 is stored at address 65492
10 is stored at address 65492
10 is stored at address 65492
4|Page
65492 is stored at address 65496
10 is stored at address 65494
Value of x: 25

Pointers and Arrays:


• When an array is declared, the compiler allocates a base address and sufficient
amount of storage to contain all the elements of the array in contiguous memory
locations.
• The base address is the location of the first element (index 0) of the array.
• The compiler also defines the array name as a constant pointer to the first element.
Suppose we declare an array x as follows:
int x[5] = {1,2,3,4,5};
Suppose the base address of x is 1000 and assuming that each integer requires 2 bytes, the
five elements will be stored as follows:

The name x is defined as a constant pointer pointing to the first element x[0] and
therefore the value of x is 1000 i.e., x = &x[0] = 1000.
Similarities between pointer and array

An array in memory Pointer pointing to an array


All the following will store value 20 into 0th element of the array.
*p = 20; p[0] = 20; *a = 20; a[0] = 20;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int eno[10],n,i;
int *e;
clrscr();
e=eno; /*intializing with base address of empno*/
5|Page
printf("Enter the no of employees:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the empno:");
scanf("%d",(e+i));
}
for(i=1;i<=15;i++)
printf("=");
printf("\n");
for(i=1;i<=n;i++)
printf("Empno[%d]=%d\n",i,*(e+i));
for(i=1;i<=15;i++)
printf("=");
getch();
}
Output
Enter the no of employees:3
Enter the empno:101
Enter the empno:102
Enter the empno:103
===============
Empno[1]=101
Empno[2]=102
Empno[3]=103
===============
Pointers and 2-D arrays:
The compiler treats a 2-d array as an collection of 1-d arrays.
int arr[3][3]={ (1,2,3),(4,5,6),(7,8,9) };

6|Page
The figure illustrates how this expression represents the element a[i][j].

➢ The base address of the array a is &a[0][0] and starting at this address, the compiler
allocates contiguous space for all the elements, row-wise.
➢ That is, the first element of the second row is placed immediately after the last element
of the first row and so on.
Suppose we declare an array a as follows:
int a[3][4] = {{15,27,11,35},{22,19,31,17},{31,23,14,36}};
The elements of a will be stored as shown below:

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2];
int i,j;

7|Page
clrscr();
printf("enter the elements:");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",(*(a+i)+j));
printf("The elements are:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%3d",*(*(a+i)+j));
printf("\n");
}
getch();
}
Output
Enter the elements:65 45 67 23
The elements are:
65 45
67 23
Array of pointers:
An array of pointers is similar to an array of any predefined data type. As a pointer
variable always contains an address, an array of pointers is a collection of addresses.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char *city[]={"vsp","chennai","hydbd","delhi"};
int i;
clrscr();
for(i=0;i<4;i++)
printf("%s\n",city[i]);
getch();
}
Output
vsp
chennai
hydbd
delhi

8|Page
Pointers to Functions:
Functions have addresses just like data items. The address of a function can be
obtained by only specifying the name of the function without the parenthesis.
Declaration of a pointer to a function:
Syntax:
return type (*ptr_variable) (functions args list);
Ex:
int test1(double)
int (*ptr) (double);
Here ptr is a pointer variable pointing to the function that returns an integer and
takes a data item of type double as its argument. The pointer ptr can point either to the
function test or test1. The expression *ptr replaces the function name in the declaration.
Example:
#include<stdio.h>
#include<conio.h>
void func1(int i, float f)
{
printf("i=%d f=%.2f\n", i,f);
}
void func2(char *s)
{
printf("s=%s",s);
}
void main()
{
void (*p)(int,float); //declaring ptrs to functions
void (*q)(char *);
int i=5;
float f=10.5;
char s[]="hai";
clrscr();
p=func1; //assigning address of func1 to p
q=func2; //assigning address of func2 to q
p(i,f); //calling the function using ptrs
q(s);
getch();
}
Output:
i=5 f=10.50
s=hai
Uses of function pointers:
➢ In passing functions as arguments to other functions
➢ In writing viruses
➢ In implementation of dynamic binding
9|Page
Dynamic Memory Allocation(DMA):
Introduction:
For example, consider a situation for the list of students in a college. The list grows
when students are admitted and shrinks when they cancel their admission. When list grows
we need to allocate more memory space to add the items. When list is shrinked then the
space is waste.
To handle such type of conditions we use a technique called Dynamic Memory
Allocation. It provides flexibility in adding, deleting or rearranging data items at runtime.
DMA:
DMA is a technique which allocates or deallocates the memory at runtime. The
process of allocating memory at runtime is called as Dynamic Memory Allocation.
➢ Compile time: Memory allocated at the time of compilation
➢ Run time: Memory allocated at the time of program execution
There are four library functions known as “Memory Management Functions” that can be
used for allocating and freeing memory during program execution.
They are: malloc( ), calloc( ), realloc( ), free( ). These functions are included in <alloc.h>
header file.
malloc():
The “malloc” or “memory allocation” method in C is used to dynamically allocate a
single large block of memory with the specified size. It returns a pointer of type void which
can be cast into a pointer of any form. It doesn’t Initialize memory at execution time so that
it has initialized each block with the default garbage value initially.
Syntax:
ptr = (cast-type*) malloc(byte-size)
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.

If space is insufficient, allocation fails and returns a NULL pointer.


10 | P a g e
Example:

#include <stdio.h>
#include <alloc.h>
int main()
{
// This pointer will hold the base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Check if the memory has been successfully allocated by malloc or not
if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
// Memory has been successfully allocated
printf("Memory successfully allocated using malloc.\n");
// Get the elements of the array
for (i = 0; i < n; ++i)
{
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
{
printf("%d, ", ptr[i]);
}
}
return 0;
}

Output:
Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,

11 | P a g e
calloc():
“calloc” or “contiguous allocation” method in C is used to dynamically allocate the
specified number of blocks of memory of the specified type. it is very much similar to malloc()
but has two different points and these are:
• It initializes each block with a default value ‘0’.
• It has two parameters or arguments as compare to malloc().
Syntax:
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each element.
Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of
the float.

If space is insufficient, allocation fails and returns a NULL pointer.


Example:

#include <stdio.h>
#include <alloc.h>
int main()
{
// This pointer will hold the base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);

12 | P a g e
// Dynamically allocate memory using calloc()
ptr = (int*)calloc(n, sizeof(int));
// Check if the memory has been successfully allocated by calloc or not
if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
// Memory has been successfully allocated
printf("Memory successfully allocated using calloc.\n");
// Get the elements of the array
for (i = 0; i < n; ++i)
{
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
{
printf("%d, ", ptr[i]);
}
}
return 0;
}

Output:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,

free():
“free” method in C is used to dynamically de-allocate the memory. The memory
allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the
free() method is used, whenever the dynamic memory allocation takes place. It helps to
reduce wastage of memory by freeing it.
Syntax:
free(ptr);

13 | P a g e
Example:

#include <stdio.h>
#include <alloc.h>
int main()
{
// This pointer will hold the base address of the block created
int *ptr, *ptr1;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Dynamically allocate memory using calloc()
ptr1 = (int*)calloc(n, sizeof(int));
// Check if the memory has been successfully allocated by malloc or not
if (ptr == NULL || ptr1 == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{

14 | P a g e
// Memory has been successfully allocated
printf("Memory successfully allocated using malloc.\n");
// Free the memory
free(ptr);
printf("Malloc Memory successfully freed.\n");
// Memory has been successfully allocated
printf("\nMemory successfully allocated using calloc.\n");
// Free the memory
free(ptr1);
printf("Calloc Memory successfully freed.\n");
}
return 0;
}

Output:
Enter number of elements: 5
Memory successfully allocated using malloc.
Malloc Memory successfully freed.
Memory successfully allocated using calloc.
Calloc Memory successfully freed.

realloc():
“realloc” or “re-allocation” method in C is used to dynamically change the memory
allocation of a previously allocated memory. In other words, if the memory previously
allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically
re-allocate memory. re-allocation of memory maintains the already present value and new
blocks will be initialized with the default garbage value.
Syntax:
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.

If space is insufficient, allocation fails and returns a NULL pointer.


15 | P a g e
Example:

#include <stdio.h>
#include <stdlib.h>
int main()
{
// This pointer will hold the base address of the block created
int* ptr;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);
// Dynamically allocate memory using calloc()
ptr = (int*)calloc(n, sizeof(int));
// Check if the memory has been successfully allocated by malloc or not
if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
// Memory has been successfully allocated
printf("Memory successfully allocated using calloc.\n");
// Get the elements of the array
for (i = 0; i < n; ++i)
{
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
{
printf("%d, ", ptr[i]);
}
// Get the new size for the array
n = 10;
printf("\n\nEnter the new size of the array: %d\n", n);
// Dynamically re-allocate memory using realloc()
ptr = realloc(ptr, n * sizeof(int));
// Memory has been successfully allocated
printf("Memory successfully re-allocated using realloc.\n");
// Get the new elements of the array

16 | P a g e
for (i = 5; i < n; ++i)
{
ptr[i] = i + 1;
}
// Print the elements of the array
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
{
printf("%d, ", ptr[i]);
}
free(ptr);
}
return 0;
}

Output:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
Enter the new size of the array: 10
Memory successfully re-allocated using realloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

Example on DMA:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int i,*a,n,p;
clrscr();
printf("Enter the size of an array:");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
printf("\nEnter the elements");
for(i=0;i<n;i++)
scanf("%d",(a+i));
printf("\nThe ELements are:");
for(i=0;i<n;i++)
printf("%d\t",*(a+i));
printf("\n\nEnter the size of extended array:");
scanf("%d",&p);
a=realloc(a,sizeof(int));
printf("\nEnter the elements");
17 | P a g e
for(i=0;i<p;i++)
scanf("%d",(a+i));
printf("\nThe ELements are:");
for(i=0;i<p;i++)
printf("%d\t",*(a+i));
free(a);
getch();
}
Output:
Enter the size of an array:3
Enter the elements
1
2
3
The ELements are:1 2 3
Enter the size of extended array:2
Enter the elements
5
2
The ELements are:4 5

Files:
When the data of the students are stored permanently, the data can be referred later and
makes the work easier for the user to generate the marks statement. When you have to store data
permanently, we have to use FILES. The files are stored in disks.
Definition:
A file can be defined as a collection of bytes stored on the disk under a name. A file
may contain anything, in the sense the contents of files may be interpreted as a collection
of records or a collection of lines or a collection of instructions etc.
A file is a collection of records. Each record provides information to the user. These
files are arranged on the disk.

File Modes of Operation:


For Text Files:
"r" (read) mode: open file for reading only.
"w" (write) mode: open file for writing only.
"a" (append) mode: open file for appending/adding data to it.
"r+" open for reading and writing, start at beginning
"w+" open for reading and writing (overwrite file)
"a+" open for reading and writing (append if file exists)
For Binary Files:
"rb" (read) mode: open binary file for reading only.
"wb" (write) mode: open binary file for writing only.

18 | P a g e
"ab" (append) mode: open binary file for adding data to it.
"rb+" open binary file for reading and writing, start at beginning
"wb+" open binary file for reading and writing (overwrite file)
"ab+" open binary file for reading and writing (append if file exists)

To access the data in a file using C we use a predefined structure FILE present in
stdio.h header file, that maintains all the information about files we create (such as pointer
to char in a file, end of file, mode of file etc).
FILE is a data type which is a means to identify and specify which file you want to
operate on because you may open several files simultaneously.
When a request is made for a file to be opened, what is returned is a pointer to the
structure FILE.
To store that pointer, a pointer variable is declared as follows:
FILE *filepointer;
where filepointer points to first character of the opened file.

Some important file operations or functions available in ‘C’ are:


Function Purpose
fopen () Creating a file or opening an existing file
fclose () Closing a file
fprintf () Writing a block of data to a file
fscanf () Reading a block data from a file
getc () Reads a single character from a file
putc () Writes a single character to a file
getw () Reads an integer from a file
putw () Writing an integer to a file
fseek () Sets the position of a file pointer to a specified location
ftell () Returns the current position of a file pointer
rewind () Sets the file pointer at the beginning of a file

Basic operations on files include:


• Naming/Creating a file
• Opening and Closing of Files
• Read data from file and Write data to file
• Read data from binary file and Write data to binary file
Naming/Creating a File:
The file name is a string of characters specified by a particular name. The file names
can be prg.txt, data.dat, add.c and so on.

19 | P a g e
Opening and Closing Files:
Opening File:
fopen():
A file needs to be opened when it is to be used for read/write operation. A file is opened
by the fopen() function with two parameters in that function. These two parameters are file
name and file open mode.
Syntax:
fptr=fopen(filename, file_open_mode);
• The fopen function is defined in the “stdio.h” header file.
• The filename parameter refers to any name of the file with an extension such as
“data.txt” or “program.c” or "student.dat" and so on.
• File open mode refers to the mode of opening the file.
The function fopen() returns the starting address of file when the file we are trying to open
is existing (i.e. success) else it returns NULL which states the file is not existing or filename
given is incorrect.
Ex: FILE *fp;
fp=fopen("data.txt","r");
Closing a File:
fclose():
A file must be closed as soon as all operations on it have been completed. This ensures
that all outstanding information associated with the file is flushed out from the buffers and
all the links to the file are broken. It also prevents any accidental misuse of file.
Syntax:
fclose(file_pointer);
• This would close the file associated with the FILE pointer file_pointer
fcloseall():
This would close all the opened files. It returns the number of files it closed.
Ex: FILE *fp;
fp=fopen("data.txt","r");
-----
-----
fclose(fp);
Ex: Program to check whether given file is existing or not using fopen() and fclose().
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp; fp=fopen("data.txt","r");
if(fp==NULL)
{
printf("No File exists in Directory"); exit(0);
}
else
printf("File Opened");
20 | P a g e
fclose(fp);
getch();
}

Reading and Writing File Operations:


Character-Oriented File I/O Functions:
• fputc()
• fgetc()
fputc():
Used to write a character onto a file.
Syntax:
fputc(ch,fptr);
where
ch represents a character
fptr is a pointer to FILE represents a file.
fgetc():
Used to read a character from a file.
Syntax:
Ch=fgetc(fptr);
where
ch represents a character
fptr is a pointer to FILE type.
Ex: Program for Writing to and reading from a file
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char c;
clrscr();
printf("Write data to file: \n"); f1 = fopen("prg.txt","w");
while((c=getchar())!='@'))
fputc(c,f1);
/*characters are stored into file until '@' is encountered*/ fclose(f1);
printf("\nRead data from file: \n");
f1 = fopen("prg.txt","r"); /*reads characters from file*/ while((c=fgetc(f1))!=EOF)
printf("%c",c); fclose(f1);
getch();
}

21 | P a g e
String-Oriented Functions:
• fputs
• fgets
fputs():
Used to write a string onto a file.
Syntax:
fputs(buffer,fptr);
where
buffer is the name of a character array
fptr is a pointer to FILE type.
fgets():
Used to raed a string from a file.
Syntax:
fgets(buffer,size,fptr);
where
buffer is the name of a character array
size is an integer value
fptr is a pointer to FILE type.
Ex: Program on fputs() and fgets()
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char file[12],text[50];
int i=0;
clrscr();
fp = fopen(“line.txt” ,”w”);
printf(“Enter text here : “);
scanf(“%s”, text);
fputs(text, fp );
fclose(fp);
fp = fopen(“line.txt”, “r”);
if(fgets(text,50,fp )!=NULL)
while(text[i]!='\0')
{
putchar(text[i]);
i++;
}
fclose(fp );
getch();
}

22 | P a g e
Data-Oriented Functions:
• fprintf()
• fscanf()
fprintf():
Used to write multiple data items.
Syntax:
fprintf(fptr,”control string”,argumentslist);
fscanf():
Used to read multiple data items.
Syntax:
fscanf(fptr,”control string”,argumentslist);
Ex:
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *f;
f = fopen("file.txt", "w");
fprintf(f, "Reading data from a file is a common feature of file handling.\n");
fclose(f);
char arr[50];
f = fopen("file.txt", "r");
while(fscanf(f, "%s", arr)!=EOF)
{
printf("%s ", arr);
}
fclose(f);
}

Reading and Writing a Binary File:


Functions fread() and fwrite() are used for reading from and writing to a file on the
disk respectively in case of binary files.
fread():
Used for writing a structure block to a given file.
Syntax:
fwrite(&structurevariable,sizeof(structure),integer,filepointer);
fwrite():
Used for reading an entire structure block from a given file.
Syntax:
fread(&structurevariable,sizeof(structure),integer,filepointer);

23 | P a g e
Ex:
#include<stdio.h>
#include<conio.h>
struct student
{
int sno;
char sname [30];
float marks;
};
void main ( )
{
struct student s[60];
int i;
FILE *fp;
fp = fopen ("student1.txt", "w");
for (i=0; i<2; i++){
printf ("enter details of student %d\n", i+1);
printf("student number:");
scanf("%d",&s[i].sno);
printf("student name:");
gets(s[i].sname);
printf("student marks:");
scanf("%f",&s[i].marks);
fwrite(&s[i], sizeof(s[i]),1,fp);
}
fclose (fp);
fp = fopen ("student1.txt", "r");
for (i=0; i<2; i++){
printf ("details of student %d are\n", i+1);
fread (&s[i], sizeof (s[i]) ,1,fp);
printf("student number = %d\n", s[i]. sno);
printf("student name = %s\n", s[i]. sname);
printf("marks = %f\n", s[i]. marks);
}
fclose(fp);
getch( );
}

24 | P a g e

You might also like