0% found this document useful (0 votes)
5 views

files

Uploaded by

jkamy180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

files

Uploaded by

jkamy180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Senior Secondary Course

Learner’s Guide: Computer Science (330)

21
FILES

 The following statement opens the file


 FILES: A file is a collection of logically STU.DAT in output mode, i.e., for writing
related records. A program usually data on the file.
requires two types of data
communication:- ofstream outfile (“STU.DAT”);
o writing data on data file
o reading data from data file Where ofstream is a class and outfile is user
defined object.
 HOW TO USE FILES? :
In C++, files are mainly dealt by using three
classes fstream, ifstream, ofstream available  outfile < < “TOTAL MARKS” << “\n”;
in fstream header file. outfile < < total << “\n”;
The statements are used for writing data on
the file. The newline character is used for
ofstream: Stream class to write on files moving the pointer to the next line.
ifstream: Stream class to read from files
fstream: Stream class to both read and write
from/to files.  ifstream infile (“STU.DAT”);

opens the file “STU.DAT” in input mode,


 WRITING DATA ON THE FILE: The i.e., for reading purpose .
data flows from keyboard to memory and
from memory to storage device.  The statements infile >> string; infile >>
Keyboard → memory → hard disk/ storage number; read the data from the data file.
device
PROGRAM:
 READING DATA FROM THE FILE: /* File Handling with C++ using ifstream
The data flows from storage device to & ofstream class object*/
memory and from memory to output
device, particularly monitor. #include <iostream>
Data file → memory → output device /* fstream header file for ifstream, ofstream,
(screen) or external storage device (hard fstream classes */
disk/storage device) #include <fstream>
 OPENING A FILE: A file can be using namespace std;
opened in two ways:
// Driver Code
o Using constructor function of a class.
int main()
o Using the member function open ( ) of {
the class // Creation of ofstream class object
ofstream fout;

1
Senior Secondary Course
Learner’s Guide: Computer Science (330)

string line;
fout.open("sample.txt");  OPENING A FILE USING OPEN() :
o First a stream object is assigned to and
// Execute a loop If file successfully then it is used to open the file in turn.
opened SYNTAX:
while (fout) { filestream_class stream_object;
stream_object . open (“filename”);
// Read a Line from standard input
getline(cin, line); For example :
ofstream outfile;
// Press -1 to exit outfile . open (“ABC”);
if (line == "-1") outfile . close ( );
break; outfile . open (“XYZ”);
outfile.close ( )
// Write line in file
fout << line << endl; o The open () function has two parameters :
} SYNTAX:
stream_object . open (“filename”, access
// Close the File mode);
fout.close();
 ios : : in for ifstream functions
// Creation of ifstream class object to read  ios : : out for ofstream functions
the file
ifstream fin;
 WRITE() & READ() FUNCTION :
fin.open("sample.txt"); The functions write ( ) and read ( ) have two
parameters: address of the variable, size of the
// Execute a loop until EOF (End of File) variable.
while (fin) { SYNTAX:
infile . read ( (char*) & v, sizeof v);
// Read a Line from File outfile . write ( (char*) & v, sizeof v); where v
getline(fin, line); is the variable.

// Print line in Console  FILE POINTERS:


cout << line << endl; File has two associated pointers called input
} pointer (or get pointer) and output pointer (or
put pointer).
// Close the file o seekg ( ) It moves get pointer to a
fin.close(); specified location.
o seekp ( ) It moves the put pointer to a
return 0; specified location.
} o ios:: beg – Beginning of the file
o ios:: cur – Current position of the
pointer
o ios:: end – End of the file
o

2
Senior Secondary Course
Learner’s Guide: Computer Science (330)

file . write ((char*) & st, sizeof st);


 tellg() & tellp() FUNCTION: } / / Display a data file
tellg ( ) - Gives the position of get pointer file . seekg ( 0, ios::beg);
in terms of number of bytes. while (file . read ((char*) & st, sizeof st))
tellp ( ) - Gives the position of put {
pointer in terms of bytes. st. putdata ( );
}
 CLOSE () FUNCTION: file . clear ( ); / / To make the end of file
mark false
stream_object.close ( ) // To append record
st . getdata ( );
 PROGRAM: file . write ((char*) & st, sizeof st);
The following program to // To modify a record
Create a data file file.clear ( );
Display a data file cout << “Enter record number”;
Adding a new record cin >> n;
Modify the existing record file . seekp ((n - 1)* sizeof st, ios::beg);
st. getdata ( );
# include file.write ((char*) & st, sizeof st);
class student // To close a file
{ file . close ( );
char name [30]; }
int rn;
public: void getdata ( );
void putdata ( ); CHECK YOURSELF
};
void student : : getdata ( )
{ cout <> name; cout << “Enter roll 1. Which header file is required to use
number”; cin >> rn; file I/O operations?
} A) <ifstream>
B) <ostream>
void student :: putdata ( ) C) <fstream>
{ D) <iostream>
cout << “Student name” << name << “\n”;
cout << “Student roll number” << rn << 2. Which of the following is not used
“\n”; as a file opening mode?
} A) ios::trunc
void main ( ) B) ios::binary
{ C) ios::in
fstream file; D) ios::ate
file . open ( “ABC”, ios::in | ios::out | 3. By default, all the files in C++ are
ios::binary); opened in _________ mode.
student st; A) Text
int i, n; B) Binary
cout << “How many records to enter”; C) ISCII
cin >> n; D) VTC
for (i = 1; i < = n, i ++ )
{
st. getdata ( );

3
Senior Secondary Course
Learner’s Guide: Computer Science (330)

ANSWERS
4. What is the return type open()
method?
A) int Answers to Check Yourself:
B) char
C) bool 1. C
D) floatis a pointer to a string 2. B
5. Which operator is used to insert the 3. A
data into file? 4. C
5. B
A) >>
B)<<
C) <
D) None of the above

STRETCH YOURSELF

1. Write a program to write and read from file?

2. Write a program using open() to read and


write a file.

3. Write a program to enter student details in


the file and display the output.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy