Chapter 11 File Operation

File Operation Long Answer Type Questions (8 Marks)

Question 1.
What are the data files? Write about their different types. (UP 2011, 12)
Or
Write a short note on ‘Sequential files’. (UP 2007, 15)
Or
Write short notes on ‘Random files’ and their application. (UP 2008, 09)
Or
Explain the features of Sequential file. (UP 2016)
Answer:
Data File: Data file offers a convenient method of storing data sets, since data files can be easily read and updated by program file (‘C’ program). Example of Data file is an employee file, which is a collection of related records.

Data files can be categorized in many ways. But we will categorize them only on the basis of mode of accessing. According to this condition, there are following two types of data files:

  1. Sequential data files
  2. Random data files.

1. Sequential Data Files: The main advantage of this type of file is that records (data) are stored sequentially, one after another, on the storage media which may be a magnetic tape or a disk. These individual data items may be different records or single entities and may comprise numerics or strings or both. If a particular data is of string type, then it must be enclosed within quotation mark.

The main disadvantage of handling the sequential file is that any particular record can be accessed sequentially. For example, if the sixtieth record from the beginning of a sequential file is to accessed, then it has to pass over the preceding fifty-nine records. Thus, it becomes more time-consuming. But if we compare it with a random data file, it is easy to create and handle.

2. Random Data Files: The basic advantage of these files is that any particular record can be accessed directly. For example, if we want to access the sixtieth record, then it can be accessed directly without passing any preceding record. It can be stored on disks only. Thus, this method is quite faster as compared to sequential data files.
For this purpose, we use function fseek. This function sets the file position indicator associated with the stream according to the values of the offset and origin. The value of origin may be one of the following:

  • 0 – the beginning of a file
  • 1 – current position
  • 2 – end of the file.

Question 2.
Discuss different file operations of ‘C’.
Answer:
File Operation: Following are the compulsory steps to operate files:

  1. Create file.
  2. Merging file i.e., combine records of two or more files into one.
  3. Adding records.
  4. Deleting records.
  5. Update records.
  6. Generate reports.

The ‘C’ language supports different level of file processing according to the environment used.
UP Board Solutions for Class 10 Computer Science Chapter 11 File Operation
The general functions which are supported by ‘C’ in the file processing are:

  • Input functions
  • Output functions

Input functions:
-getc()
-fgetc()
-fscanf()
-fread()

Output functions:
-putc()
-fputc()
-fprintf()
-fwrite()

fopen( ) is the main function to start the above given functions. To open a file, to create a file, to restart with existing file etc., are the main functions which are performed by it.
Syntax:
file pointer = fopen(filename, mode);
i.e., FILE *fp;
fp = fopen(TRY.C”, “r”);
fp is a pointer variable, which contains the address of the structure FILE which has been defined in the header file “stdio.h”.
fopen() will open a file TRY.C’ in ‘read* mode, which tells the C compiler that we would be reading the contents of the file, “r” is a string and not a character. Hence, fopen() performs three important tasks when you open the file in “r” mode:

  • It searches on the disk the file to be opened.
  • If the file is present, it loads that file from the disk into memory.
    If the file is absent, fopen() returns a NULL. This function opens one file at a time.
  • It sets up a character pointer which points to the first character of the memory where the file has been modified.

Question 3.
Write about different modes in which a file can be opened.
Answer:
File Opening Modes: The tasks performed by fopen() when a file is opened in each of these modes are also mentioned.
Character Used
for Mode → Meaning
w → Create for write
r → Open for reading
a → Open for append/create a new file
w+ → Create for read/write
r+ → Open for read/write
a+ → Open for read/write/create new file.

Question 4.
WAP to copy the contents of one file to another?
Answer:
Program:

#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fsrc, *ftgt;

char ch;

clrscr();

fsrc = fopen(“tiyl.c”, “r”);

if (fs = = NULL)

{

puts (“Cannot open source file”);

exit();

}

ftgt = fopen(“tiy2.c”, “w”);

if (ft = = NULL)

{

puts (“Cannot open target file”);

fclose(fs);

exit();

} .

while(1)

{ . ch = fgetc(fsrc);

if (ch = = EOF)

break;

else

fputc(ch, ftgt);

}

fclose(fsrc);

fclose(ftgt);

}

Question 5.
Explain fread () and fwrite () functions.
Answer:
fread( ): This function reads a specified number of equal-sized data items from an input stream into a block, fread returns the number of items (not bytes) actually read on success.
Syntax:
fread (& name_of_structure, size_of_(structure name), l, file pointer);
e.g.,

struct employee

{

char nm[20]; /* 20 bytes */

int age; /* 2 bytes */

};

struct employee Emp;

fread (&Emp, size of (Emp), 1, fp);

Here, the fread function can read 22 bytes of information from the file pointed by file pointer fp.
fwrite( ): This function appends a specified number of equal-sized data items to an output file.
Syntax:
fwrite (& struct—name, size of (struct), 1, fp);
e.g.,

street address

{

char city [30]; /* 30 bytes */

long in pin; /* 2 bytes */

char country [20]; /* 20 bytes */

};

struct address add;

FILE *fp;

fwrite (& add, size of (add), 1, fp);

Question 6.
Describe the main features of printf and getchar function. (UP 2014)
Answer:
printf ( ) → This function writes formatted data to screen. This function allows to supply the input in a fixed format and to obtain the output in the specified form. The printf () function interprets the contents of the format string.
Syntax:
Printf (“formatted string”, variables); if needed
Example 1.
Printf (“Average = % d percentage = % of’, avg. per);
Here the %d and %f are conversion characters.
They tell printf () to print the value of avg as an integer and per as afloat.

getchar ( ): Using this function any single ASCII character can be inputted in any char type of a variable. This function is available in stdio.h header file so it must be included in the program, in the beginning, using # include declarative.
For example,

#include<stdio.h>

#include<conio.h>

void main ()

{

char x;

x = getchar();

cout << “you entered” << x;

}

File Operation Short Answer Type Questions (4 Marks)

Question 1.
What is stdin and what is stdout?
Answer:
In ‘C’ language, whatever values are given or displayed on a monitor, it can be file handling first stored in a buffer area. The two buffer areas used for this are:

  • stdin: It stands for keyboard buffer as it stores the information sent by the keyboard.
  • stdout: It stands for standard output buffer, le., monitor of the computer.

Question 2.
Differentiate fprintf() and fscanf(). (UP 2016)
Answer:
fprintf( ): This function sends formatted output to a stream. It accepts a series of arguments, apply to each argument a format specifier contained in the format string * format. This function applies the first format specifier to the first argument, the second specifier to the second argument… till the end of the format.

fscanf(): This function is similar to scanf() function. It scans a series of input fields one character at a time. Store the formatted input at an address passed as an argument following * format. This function might stop scanning a particular field before it reaches the normal end-of-field (while space) character, or it might terminate entirely.

Question 3.
What is reading files? (UP 2008)
Answer:
Reading from files: To read from a file, ‘C’ provides the following functions:
To read data from file, it should exist.
fscanf (): This function is used to read data from a file. It is very similar to scanf () function. It scans a series of input fields (one at a time).
Syntax:
fscanf () file * stream, “format specifiers”, “Variables”).

Question 4.
What is the closing of the file?
Answer:
Closing Files: During a write to a file, the data written is not put on the disk immediately. It is stored in a buffer. When the buffer is full, all its contents are actually written to the disk. The process of emptying the buffer by writing its contents to disk is called flushing the buffer. Losing the file flushes the buffer and releases the space taken by the FILE structure which is returned by fopen. For this fclose () function is used.
Syntax:
fclose (File * stream (s);

File Operation Very Short Answer Type Questions (2 Marks)

Question 1.
In which type of file, accessing a particular record is faster?
Answer:
Random data file.

Question 2.
Which statement is used to close a file?
Answer:
fclose() statement.

Question 3.
When data is to be written on a file, in which mode it should be opened? (UP 2016)
Answer:
‘w’ mode (for writing).

Question 4.
Which statement is used to read the data from a file?
Answer:
fread() statement is used to read the data from a file.

Question 5.
What do you know about ffiush() statement?
Answer:
If the given stream has buffered output, fflush writes the output for a stream to the associated file.

Question 6.
Which is function prints error message corresponding to the last Library routine that produced the error?
Answer:
perror ().

Question 7.
Which file pointer position of seeking function seeks from the beginning?
Answer:
SEEK SET.

File Operation Objective Type Questions (1 Marks)

There are four alternative answers for each part of the questions. Select the correct one and write in your answer book:

Question 1.
The last location of a file is:
(a) EOF
(b) END
(c) LAST
(d) CLOSE.
Answer:
(a) EOF

Question 2.
Which function is used to close a file? (UP 2015)
(a) CLOSE
(b) END
(c) EOF
(d) None of these.
Answer:
(b) END

Question 3.
Which function is used to read a line from file:
(a) gets ()
(b) fgets ()
(c) readline ()
(d) gets ().
Answer:
(b) fgets ()

Question 4.
Which function sets the file pointer associated with a stream to a new position:
(a) fseek ()
(b) perror
(c) rename
(d) rewind.
Answer:
(a) fseek ()