File Handling in C++

20 %
80 %
Information about File Handling in C++
Science-Technology

Published on December 29, 2010

Author: tindo

Source: authorstream.com

File Handling in C++ : File Handling in C++ File Handling in C++ : File Handling in C++ Introduction to File Handling Data entered once and required later again. Same data to be used by others. Data required again by the same program. Files and Streams : Files and Streams Program          Input Stream Output Stream           Files I/O Streams : I/O Streams Stream Description cin Standard input stream cout Standard output stream cerr Standard error stream File I/O Streams : File I/O Streams Stream classes required for File I/O : ifstream ofstream fstream ifstream : ifstream Input file stream class Open() is a member function of the class ifstream Inherited functions of ifstream class from the class istream are: get() getline() read() seekg() tellg() Ofstream : Ofstream Output file stream class. Open() is a member function of the class ofstream. Inherited functions of ofstream class from the class ostream are put() write() seekp() tellp() fstream : fstream Supports files for simultaneous input and output. fstream is derived from ifstream ofstream iostream They are all parent class and fstream is the child class Fstream : Fstream Member functions for fstream are: open close closeall seekg seekp tellg tellp Opening a File : Opening a File A file can be opened by using any of the following two ways: Using the constructor function of the class Using the member function open() of the class OPENING A FILE USING CONSTRUCTOR: : OPENING A FILE USING CONSTRUCTOR: As we know a constructor is used to initialize an object while it is being created. Same as we can open a file by supplying the name of the file to the constructor of the file stream as follows: Example: ofstream obj(“abc.jpg”); ifstream file(“LETTER.TXT”); fstream fileio(“answers.txt”); OPENING A FILE USING OPEN() : OPENING A FILE USING OPEN() As we know the function open() is the member function in all the file streams ( ifstream,ofstream & fstream). We can open a file of a particular type of file stream with its default mode or by providing the other modes explicitly, as follows: Example: ifstream obj; obj.open(“STORY.TXT”); ofstream file; file.open(“product.dat”); fstream abc; abc.open(“xyz.dat”,ios::binary|ios::io|ios::out); CLOSING A FILE: : CLOSING A FILE: A file is closed using the member function close(), as follows: Example: ifstream obj; ----------- ----------- obj.close(); FILE MODES: : FILE MODES: The file mode parameter in open() and constructor function specifies the purpose for which the file is opened. If we don’t supply the mode parameter when we open a file it uses the default values in the absence of the actual value. The default values are as follows: ios::in for ifstream functions meaning open for reading only. ios::out for ofstream functions meaning open for writing only. OTHER FILE MODES & THEIR MEANING: : OTHER FILE MODES & THEIR MEANING: Parameter Meaning ios::app - Append to end of file. ios::ate - Go to end of file on opening ios:binary - Binary file ios::in - open file for reading only ios:nocreate - open fails if the file does not exist ios::noreplace - open fails if the file already exists ios::out - open file for writing only ios::trunc - delete the contents of the file if it exists SEQUENTIAL INPUT AND OUTPUT OPERATIONS: :  SEQUENTIAL INPUT AND OUTPUT OPERATIONS: 1.The file stream classes support a number of member functions for performing the input and output operations on files. There are two pair of functions for this purpose-put() & get() functions : these functions are designed for handling a single character at a time. syntax: obj.put(ch); obj.get(ch); SEQUENTIAL INPUT AND OUTPUT OPERATIONS: : SEQUENTIAL INPUT AND OUTPUT OPERATIONS: 2. write() & read() functions: these functions are designed to write and read blocks of binary data. syntax: obj.write((char*)&var, sizeof(var));obj.read((char*)&var, sizeof(var)); Here var is an variable of either class or structure.

Add a comment

Related presentations

Related pages

File Handling in C Language | C Language Tutorial ...

C Language provides functions like fopen, fread, fwrite, fseek to make file Handling in C language simple. Lets learn How to Open a file, close a file ...
Read more

File Handling in C Language - C and C++ Programming Resources

Files handling in C Programming Language ... Appending (a) When a file is opened for appending, it will be created if it does not already exist and it will ...
Read more

File Handling in C Language - Classle-Social learning with ...

File Handling in C Language In this section, we will discuss about files which are very important for storing information permanently. We store information ...
Read more

File Handling in C with Examples (fopen, fread, fwrite, fseek)

As with any OS, file handling is a core concept in Linux. Any system programmer would learn it as one of his/her initial programming assignments. This aspe
Read more

C Programming Examples on File Handling - Code, Output ...

This section covers C programming examples on File Handling. Every example program includes the description of the program, C code as well as output of the ...
Read more

File Handling

Use these routines to create, delete, and manipulate files and to set and check file-access permissions. The C run-time libraries have a 512 limit for the ...
Read more

File Handling in C - Index - c4learn.com

Enter your email address: Recent from c4learn class. Traditional Database Systems; Python dictionary – clear() : method
Read more

File Handling In C - English - YouTube

Files in C -File handling functions -Opening a File closing a file --example: fopen, fclose -Reading data from a File.
Read more

File Handling in C - C Tutorial | Intellipaat.com

File handling is used to store the data permanently in files. It is used to open, store, read, search or close files.
Read more

38 | File Handling in C Programming Language Video Tutorial

In this C programming language video tutorial / lecture for beginners video series, you will learn about the concept of files, how to Create and ...
Read more