Chapter 10
Chapter 10
Chapter 10
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.
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.
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("Test.txt","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;
}
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:\\newfile.txt","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;
}
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>
int main()
char filename[100], c;
scanf("%s", filename);
if (fptr1 == NULL)
exit(0);
7
PPS (3110003)
scanf("%s", filename);
if (fptr2 == NULL)
exit(0);
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
fclose(fptr1);
fclose(fptr2);
return 0;
Output:
8
PPS (3110003)
a.txt
b.txt
Q. 9 Write C program to copy content of file “D:\home\avs.txt” into new textfile “ftel.txt” 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)
(CPU_SUM_2016)
10