SlideShare a Scribd company logo
c++ tutorial for school students




Home
Tutorials
Papers
Projects
Assignments
FAQ
About Us
Contact Us
Navigation
Home
Tutorials
Papers
Projects
Assignments
CBSE Links
Results
Syllabus



            Previous                                  INDEX                                      Next


                                          DATA FILE HANDLING IN C++

File. The information / data stored under a specific name on a storage device, is called a file.
Stream. It refers to a sequence of bytes.
Text file. It is a file that stores information in ASCII characters. In text files, each line of text is terminated
with a special character known as EOL (End of Line) character or delimiter character. When this EOL
character is read or written, certain internal translations take place.
Binary file. It is a file that contains information in the same format as it is held in memory. In binary files,
no delimiters are used for a line and no translations occur here.
Classes for file stream operation
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Opening a file
OPENING FILE USING CONSTRUCTOR
ofstream fout(“results”); //output only
ifstream fin(“data”); //input only
OPENING FILE USING open()
Stream-object.open(“filename”, mode)
     ofstream ofile;
     ofile.open(“data1”);

    ifstream ifile;
    ifile.open(“data2”);
             File mode parameter                Meaning


             ios::app                           Append to end of file



             ios::ate                           go to end of file on opening



             ios::binary                        file open in binary mode



             ios::in                            open file for reading only



             ios::out                           open file for writing only



             ios::nocreate                      open fails if the file does not exist



             ios::noreplace                     open fails if the file already exist



             ios::trunc                         delete the contents of the file if it exist

All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file
example.bin in binary mode to add data we could do it by the following call to member function open():
fstream file;
file.open ("example.bin", ios::out | ios::app | ios::binary);
CLOSING FILE
  fout.close();
  fin.close();
INPUT AND OUTPUT OPERATION
put() and get() function
the function put() writes a single character to the associated stream. Similarly, the function get() reads a
single character form the associated stream.
example :
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
ERROR HANDLING FUNCTION
             FUNCTION        RETURN VALUE AND MEANING


                             returns true (non zero) if end of file is encountered while reading; otherwise return
             eof()
                             false(zero)



             fail()          return true when an input or output operation has failed



                             returns true if an invalid operation is attempted or any unrecoverable error has
             bad()
                             occurred.



             good()          returns true if no error has occurred.

FILE POINTERS AND THEIR MANIPULATION
All i/o streams objects have, at least, one internal stream pointer:
ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the
next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next
element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both
istream and ostream).

These internal stream pointers that point to the reading or writing locations within a stream can be
manipulated using the following member functions:

             seekg()           moves get pointer(input) to a specified location



             seekp()           moves put pointer (output) to a specified location



             tellg()           gives the current position of the get pointer



             tellp()           gives the current position of the put pointer



The other prototype for these functions is:
seekg(offset, refposition );
seekp(offset, refposition );
The parameter offset represents the number of bytes the file pointer is to be moved from the location
specified by the parameter refposition. The refposition takes one of the following three constants defined in
the ios class.
ios::beg           start of the file
ios::cur          current position of the pointer
ios::end           end of the file
example:
file.seekg(-10,   ios::cur);

           Previous                                 INDEX                    Next




By Deepak Singh
Home | Tutorials | Papers | Projects | Assignments | Contact us | About us

More Related Content

PDF
Chapter28 data-file-handling
PPTX
Stream classes in C++
PPTX
2CPP17 - File IO
DOCX
File handling in c++
PPT
17 files and streams
PDF
C++ Files and Streams
PPT
working file handling in cpp overview
Chapter28 data-file-handling
Stream classes in C++
2CPP17 - File IO
File handling in c++
17 files and streams
C++ Files and Streams
working file handling in cpp overview

What's hot (20)

PPTX
File handling
PDF
Files and streams
PPT
Filehandlinging cp2
PDF
Filehadnling
PPTX
File Handling
PDF
08. handling file streams
PPTX
9 Inputs & Outputs
PPT
File handling in C++
PPSX
Files in c++
PPT
File handling in_c
PPTX
Data file handling in c++
PPTX
Files in c++
PPTX
Pf cs102 programming-8 [file handling] (1)
PPT
C++ files and streams
PPTX
File handling in c++
PPT
Cpp file-handling
PPTX
basics of file handling
File handling
Files and streams
Filehandlinging cp2
Filehadnling
File Handling
08. handling file streams
9 Inputs & Outputs
File handling in C++
Files in c++
File handling in_c
Data file handling in c++
Files in c++
Pf cs102 programming-8 [file handling] (1)
C++ files and streams
File handling in c++
Cpp file-handling
basics of file handling
Ad

Viewers also liked (6)

PDF
PDF
PPTX
Sosial media kampanje
PDF
Ajax 応用
PDF
Resumen09a
PDF
Tourist destination of India
Sosial media kampanje
Ajax 応用
Resumen09a
Tourist destination of India
Ad

Similar to Filehandling (20)

PPTX
Basics of file handling
PDF
Filesinc 130512002619-phpapp01
PPTX
File management in C++
PPTX
Programming Fundamentals lecture-22.pptx
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
PDF
Data file handling
PDF
File Handling.pdffile handling ppt final
PDF
Filepointers1 1215104829397318-9
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
PPT
file_handling_in_c.ppt......................................
PDF
Files in C++.pdf is the notes of cpp for reference
PPTX
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
PPTX
pointer, structure ,union and intro to file handling
PPT
PL-II Lecture19 oop in c++ pyaray bachon .ppt
PPT
PL-II Lecture19 oop in c++ pyaray bachon .ppt
PPT
7 Data File Handling
PPTX
Unit iv
PPTX
C++ - UNIT_-_V.pptx which contains details about File Concepts
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Basics of file handling
Filesinc 130512002619-phpapp01
File management in C++
Programming Fundamentals lecture-22.pptx
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
Data file handling
File Handling.pdffile handling ppt final
Filepointers1 1215104829397318-9
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
file_handling_in_c.ppt......................................
Files in C++.pdf is the notes of cpp for reference
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
PL-II Lecture19 oop in c++ pyaray bachon .ppt
PL-II Lecture19 oop in c++ pyaray bachon .ppt
7 Data File Handling
Unit iv
C++ - UNIT_-_V.pptx which contains details about File Concepts
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS

Filehandling

  • 1. c++ tutorial for school students Home Tutorials Papers Projects Assignments FAQ About Us Contact Us Navigation Home Tutorials Papers Projects Assignments CBSE Links Results Syllabus Previous INDEX Next DATA FILE HANDLING IN C++ File. The information / data stored under a specific name on a storage device, is called a file. Stream. It refers to a sequence of bytes. Text file. It is a file that stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character. When this EOL character is read or written, certain internal translations take place. Binary file. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here. Classes for file stream operation ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.
  • 2. Opening a file OPENING FILE USING CONSTRUCTOR ofstream fout(“results”); //output only ifstream fin(“data”); //input only OPENING FILE USING open() Stream-object.open(“filename”, mode) ofstream ofile; ofile.open(“data1”); ifstream ifile; ifile.open(“data2”); File mode parameter Meaning ios::app Append to end of file ios::ate go to end of file on opening ios::binary file open in binary mode ios::in open file for reading only ios::out open file for writing only ios::nocreate open fails if the file does not exist ios::noreplace open fails if the file already exist ios::trunc delete the contents of the file if it exist All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open(): fstream file; file.open ("example.bin", ios::out | ios::app | ios::binary); CLOSING FILE fout.close(); fin.close(); INPUT AND OUTPUT OPERATION put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream. example : file.get(ch); file.put(ch);
  • 3. write() and read() function write() and read() functions write and read blocks of binary data. example: file.read((char *)&obj, sizeof(obj)); file.write((char *)&obj, sizeof(obj)); ERROR HANDLING FUNCTION FUNCTION RETURN VALUE AND MEANING returns true (non zero) if end of file is encountered while reading; otherwise return eof() false(zero) fail() return true when an input or output operation has failed returns true if an invalid operation is attempted or any unrecoverable error has bad() occurred. good() returns true if no error has occurred. FILE POINTERS AND THEIR MANIPULATION All i/o streams objects have, at least, one internal stream pointer: ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation. ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written. Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both istream and ostream). These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions: seekg() moves get pointer(input) to a specified location seekp() moves put pointer (output) to a specified location tellg() gives the current position of the get pointer tellp() gives the current position of the put pointer The other prototype for these functions is: seekg(offset, refposition ); seekp(offset, refposition ); The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. The refposition takes one of the following three constants defined in the ios class.
  • 4. ios::beg start of the file ios::cur current position of the pointer ios::end end of the file example: file.seekg(-10, ios::cur); Previous INDEX Next By Deepak Singh Home | Tutorials | Papers | Projects | Assignments | Contact us | About us