Files in C
Files in C
Files
16.1 Introduction
At times it is required to store data on hard disk or floppy disk in some application
program. The data is stored in these devices using the concept of file.
16.2 Objectives
After going through this lesson, you would be able to:
l store data in a file
16.3 File
A file is a collection of logically related records. A program usually requires two
types of data communication.
The data flows from keyboard to memory and from memory to storage device.
This is called output stream where stream is the flow of data and requires an
ofstream.h header file.
The data flows from storage device to memory and from memory to output device,
particularly monitor.
external storage
device
(hard disk/floppy)
If both input stream and output stream are used in the same program then header
file fstream.h is required.
The following statement opens the file STU.DAT in output mode, i.e., for writing data
on the file.
The statements
are used for writing data on the file. The newline character is used for moving the
pointer to the next line.
opens the file “STU.DAT” in input mode, i.e., for reading purpose
The statements
The following program uses a single file for both writing and reading purposes. First,
it takes the data form the keyboard and writes it to the file. After the writing is
completed, the file is closed. The program again opens the same file, reads the
information already written to it and displays it on the screen.
The function open ( ) can be used to multiple files that use the same stream object.
First a stream object is assigned to and then it is used to open the file in turn.
filestream_class stream_object;
stream_object . open (“filename”);
For example :
ofstream outfile;
outfile . open (“ABC”);
_____
outfile . close ( );
outfile . open (“XYZ”);
_____
outfile.close ( );
format is:
The second argument specifies the mode in which the file is opened. The default
values are taken for ifstream or ofstream functions. (the mode is not defined
explicitly).
The file mode parameters can take one or more of the constants defined in the
class ios. The following table shows the file mode parameters.
Parameter Meaning
The mode can combine two or more parameters using bitwise OR opertor.
Example:
The refposition takes one of the above three constants defined in the ios class.
Example 1
It moves the pointer to the beginning of the file. In this case, the refposition ios ::
beg is optional.
It moves the pointer 100 bytes forward from the current position.
It moves the pointer 200 bytes backward from the end of the file.
Example 2
ifstream infile;
On execution of the above statements, the input pointer is moved to the end of the
file and B gives the number of bytes in the file.
The following example works with class object and does the following operations:
Example 3
# include <fstream.h>
class student
{
char name [30];
int rn;
public:
void getdata ( );
void putdata ( );
};
void student : : getdata ( )
{
cout <<“Enter student name”;
cin >> name;
cout << “Enter roll number”;
cin >> rn;
}
void student :: putdata ( )
{
cout << “Student name” << name << “\n”;
cout << “Student roll number” << rn << “\n”;
}
void main ( )
{
fstream file;
file . open ( “ABC”, ios::in l ios::out l ios::binary);
student st;
/ / create a data file
int i, n;
cout << “How many record to enter”;
cin >> n;
for (i = 1; i < = n, i ++ )
{
st. getdata ( );
(c) The output mode of opening a file deletes the contents, if present in the
file.
(f) The ios::ate mode allow us to write data at the end the file only.
(h) The data written to a file with write ( ) function can be read with the get
( ) function.
2. What is the difference between opening a file with constructor function and
3. What is the file access mode? Describe the various file modes.
fstream file:
N = file.tellg ( );
fstream file;
file.open (“ABC”, ios::in l ios::out);
Write C++ statement(s) for the following:
(i) To move the pointer at the beginning of file.
(ii) To move the pointer at the end of file.
(iii) To find the total number of bytes.
(iv) To close the file.
fstream file;
(i) file.seekg (100, ios::cur);
(ii) file.seekg (-100, ios::end);
(iii) file.seekg ( 100, ios::beg);
7. The record consists of two fields: name and rollno. Write a program that will
perform the following:
2. The ios::ate mode can add or modify an existing record whereas ios::app
mode can only add records.
4. True
6. (a) file
(b) output
(c) input
(d) open
(e) output
(f) trunc
7. (a) T
(b) T
(c) T
(d) T
(e) T
(f) F
(g) T
(h) F