PPS (3110003)
CHAPTER 10
File management
Q.1 Describe file management. And List the various file management functions. Explain
the various file modes. (PPS_WIN2019) (CPU_WIN_2015) (CPU_WIN_2014)
(CPU_WIN_2019) (CPU_WIN_2013) (CPU_SUM_2014) (CPU_SUM_2015)
(CPU_SUM_2018)
A file management system is a type of software that manages data files in a computer
system.
Different file management functions:
There are many functions in the C library to open, read, write, search and close the file. A
list of file functions are given below.
fopen() - opens new or existing file
fprintf() - write data into the file
fscanf() - reads data from the file
fputc() - writes a character into the file
fgetc() - reads a character from file
fclose() - closes the file
fseek() - sets the file pointer to given position
fputw() - writes an integer to file
fgetw() - reads an integer from file
ftell() - returns current position
rewind() - sets the file pointer to the beginning of the file
various file modes
r - opens a text file in read mode
w - opens a text file in write mode
a - opens a text file in append mode
r+ - opens a text file in read and write mode
w+ - opens a text file in read and write mode
a+ - opens a text file in read and write mode
rb - opens a binary file in read mode
wb - opens a binary file in write mode
ab - opens a binary file in append mode
rb+ - opens a binary file in read and write mode
wb+ - opens a binary file in read and write mode
ab+ - opens a binary file in read and write mode
1
PPS (3110003)
Q.2 Write a program to illustrate the use of fputc ( ) and fputs( ) .(PPS_WIN2019)
(CPU_WIN_2016)
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char ch[4]={'a','b','c','d'};
int i;
fp=fopen("[Link]","w");
if(fp == NULL)
{
perror("Error opening file");
return(-1);
}
for(i=0;i<4;i++)
{
fputc(ch[i],fp); //use of fputc()
}
fputs("EFGH",fp); //use of fputs()
fclose(fp);
return 0;
}
Q.3 Explain getw() and putw() function with example. .(CPU_WIN_2019)
putw(), getw() functions are file handling function in C programming language which is used to
write an integer value into a file (putw) and read integer value from a file (getw).
putw()
Declaration: int putw(int number, FILE *fp);
putw function is used to write an integer into a file. In a C program, we can write integer value in
a file as below.
putw(i, fp);
where
i – integer value
fp – file pointer
getw()
Declaration: int getw(FILE *fp);
2
PPS (3110003)
getw function reads an integer value from a file pointed by fp. In a C program, we can read integer
value from a file as below.
getw(fp);
This file handling C program illustrates how to write into a file using putw() function and how to
read the same file using getw() function.
#include <stdio.h>
int main ()
{
FILE *fp;
int i=1, j=2, k=3, num;
fp = fopen ("test.c","w");
putw(i,fp);
putw(j,fp);
putw(k,fp);
fclose(fp);
fp = fopen ("test.c","r");
while(getw(fp)!=EOF)
{
num= getw(fp);
printf(“Data in test.c file is %d \n”, num);
}
fclose(fp);
return 0;
}
OUTPUT:
Data in test.c file is
1
2
3
3
PPS (3110003)
Q.4 Explain fopen() and its mode with example to write a string into file
A file is opened using fopen, which returns an I/O stream attached to the specified file or other
device from which reading and writing can be done. If the function fails, it returns a null pointer.
They are defined as
FILE *fopen(const char *path, const char *mode);
MODES SAME AS
If the file doesn’t exist then this program will create a file with the specified name and writes the
input character into the file.
#include <stdio.h>
int main()
{
char ch;
FILE *fpw;
fpw = fopen("C:\\[Link]","w");
if(fpw == NULL)
{
printf("Error");
exit(1);
}
printf("Enter any character: ");
scanf("%c",&ch);
/* You can also use fputc(ch, fpw);*/
fprintf(fpw,"%c",ch);
fclose(fpw);
return 0;
}
Q.5 (a) Explain fopen ( ) and fclose ( ) file handling functions.(CPU_WIN_2017)
fopen()
Declaration: FILE *fopen (const char *filename, const char *mode)
4
PPS (3110003)
fopen() function is used to open a file to perform operations such as reading, writing etc. In a C
program, we declare a file pointer and use fopen() as below. fopen() function creates a new file if
the mentioned file name does not exist.
FILE *fp;
fp=fopen (“filename”, ”‘mode”);
Where,
fp – file pointer to the data type “FILE”.
filename – the actual file name with full path of the file.
mode – refers to the operation that will be performed on the file. Example: r, w, a, r+, w+ and a+.
Please refer below the description for these mode of operations.
fclose()
Declaration: int fclose(FILE *fp);
fclose() function closes the file that is being pointed by file pointer fp. In a C program, we close a
file as below.
fclose (fp);
Q.6 Write a program to print the program itself. .(CPU_WIN_2017)
#include <stdio.h>
int main(void) {
// to print the source code
char c;
// __FILE__ gets the location
// of the current C program file
FILE *file = fopen(__FILE__, "r");
do {
//printing the contents
//of the file
c = fgetc(file);
putchar(c);
}
while (c != EOF);
fclose(file);
5
PPS (3110003)
return 0;
}
Output
#include <stdio.h>
int main(void) {
// to print the source code
char c;
// __FILE__ gets the location
// of the current C program file
FILE *file = fopen(__FILE__, "r");
do {
//printing the contents
//of the file
c = fgetc(file);
putchar(c);
}
while (c != EOF);
fclose(file);
return 0;
}
Q. 7 Write syntax of fseek() function and explain fseek(fp,-10,1) and fseek(fp,10,0).
(CPU_SUM_2016)
fseek() is used to move file pointer associated with a given file to a specific position.
Syntax:
int fseek(FILE *pointer, long int offset, int position)
pointer: pointer to a FILE object that identifies the stream.
offset: number of bytes to offset from position
position: position from where offset is added.
6
PPS (3110003)
returns:
zero if successful, or else it returns a non-zero value
position defines the point with respect to which the file pointer needs to be moved. It has three
values:
SEEK_END:It denotes end of the file.
SEEK_SET:It denotes starting of the file.
SEEK_CUR : It denotes file pointer’s current position.
fseek(fp,-10,1)
• Seek (find) to 10th byte before the end of the file.
fseek(fp,10,0)
• Seek (find) to the 100th byte of the file.
Q. 8 Write a ‘C’ program to which copies the contents of one file to other. (CPU_SUM_2017)
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
printf("Cannot open file %s \n", filename);
exit(0);
7
PPS (3110003)
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
printf("Cannot open file %s \n", filename);
exit(0);
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
Output:
8
PPS (3110003)
Enter the filename to open for reading
[Link]
Enter the filename to open for writing
[Link]
Contents copied to [Link]
Q. 9 Write C program to copy content of file “D:\home\[Link]” into new textfile “[Link]” in
E drive . (CPU_SUM_2019)
#include <stdio.h>
#include <stdlib.h>
main()
{
char ch, source_file[20], target_file[20];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if( source == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
9
PPS (3110003)
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
return 0;
}
MCQ
(CPU_WIN_2016) (CPU_WIN_2014)
[Link] manipulation functions in C are available in which header file?
(a) streams.h (b) stdio.h (c) stdlib.h (d) files.h
(CPU_SUM_2016)
Q. When fopen() fails to open a file it returns
(a) NULL (b) 1 (c) -1 (d) None of above
10