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

Academic Year 2021 – 2022

Question Bank
Year/Semester: Department: CSE Unit: I/II/III/IV/V
I/ II
Subject Code/Title : CS3251 / Programming in C Section: Part A/B/C
Date:04/04/2022 Faculty Name: Ms.A.Kanimozhi

UNIT V
Part A:

1. Why files are needed?


o When a program is terminated, the entire data is lost. Storing in a file willpreserve your data even
if the program terminates.

2. What are the types of Files ?


When dealing with files, there are two types of files you should knowabout:
o Text files
o Binary files
Text files
Text files are the normal .txt files that you can easily create usingNotepad or any simple text editors.
When you open those files, you'll see all the contents within the file asplain text. You can easily edit
or delete the contents.
They take minimum effort to maintain, are easily readable, and provideleast security and takes
bigger storage space.
Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0'sand 1's).
They can hold higher amount of data, are not readable easily and providesa better security than text
files.

3. Enlist the File Operations.


In C, you can perform four major operations on the file, either text orbinary:
o Creating a new file
o Opening an existing file
o Closing a file
o Reading from and writing information to a file
Working with files
When working with files, you need to declare a pointer of type file. Thisdeclaration is needed for
communication between the file and program. FILE *fptr;
4. How to open a file?
o Opening a file is performed using the library function in the "stdio.h"
header file: fopen().
o The syntax for opening a file in standard I/O is:ptr =
fopen("fileopen","mode")
5. List the opening modes in standard I/O
If the file does not exist, fopen()returns NULL.
r Open for reading.
Open for reading in binarymode. If the file does not exist, fopen()returns NULL.
rb
If the file exists, its contents areoverwritten. If the
w Open for writing. file does not exist, it will becreated.
If the file exists, its contents areoverwritten. If the
wb Open for writing in binarymode. file does not exist, it will becreated.
Open for append. i.e, Datais added
A to end of file. If the file does not exists, it willbe created.
Open for append in binarymode.
ab i.e, Data is added to end of file. If the file does not exists, it willbe created.
Open for both reading andwriting. If the file does not exist, fopen()returns NULL.
r+
Open for both reading andwriting
rb+ in binary mode. If the file does not exist, fopen()returns NULL.
If the file exists, its contents areoverwritten. If the
w+ Open for both reading andwriting. file does not exist, it will becreated.

If the file exists, its contents areoverwritten. If the


wb+ Open for both reading andwriting file does not exist, it will becreated.
in binary mode.
Open for both reading and If the file does not exists, it willbe created.
a+ appending.
Open for both reading and
ab+ appending in binary mode. If the file does not exists, it willbe created.

6. How to close a file?


The file (both text and binary) should be closed after reading/writing.Closing a file is
performed using library function fclose(). fclose(fptr); //fptr is the file pointer associated
with file to be closed.

7. Reading and writing to a text file


For reading and writing to a text file, we use the functions fprintf() andfscanf().
They are just the file versions of printf() and scanf(). The only differenceis that, fprint and
fscanf expects a pointer to the structure FILE.

8. What are two main ways a file can be organized?


Sequential Access — The data are placed in the file in a sequence likebeads on a string. Data are
processed in sequence, one after another. To reach a particular item of data, all the data that
proceeds it first must be read.
Random Access — The data are placed into the file by going directly to the location in the file
assigned to each data item. Data are processed inany order.
A particular item of data can be reached by going directly to it, withoutlooking at any other
data.
9. What is file?
A file is a semi-permanent, named collection of data. A File is usuallystored on magnetic
media, such as a hard disk or magnetic tape.
Semi-permanent means that data saved in files stays safe until it is deletedor modified.
Named means that a particular collection of data on a disk has a name,like mydata.dat and
access to the collection is done by using its name.
10. State Report Generation.
A very common data processing operation is report generation. This is when the data in a file
(or files) is processed by a program to generate a report of some kind. The report might be a

You might also like