Home     Blog

File Manipulation

A file is basically a set of records stored on your computer's secondary memory. There may be situations requiring a program to open more than one file. The strategy for opening multiple files depends upon how they will be used. If the situation requires simultaneous processing of two files, then you need to create a separate stream for each file. However, if the situation demands sequential processing of files (i.e., processing them one by one), then one can open a single stream and associate it with each file in turn. To use this approach, declare a stream object without initializing it, and then use a second statement to associate the stream with a file.

For instance,

The above code lets you read two files in succession. Note that, the first file is closed before opening the second file. This is necessary because a stream can be connected to only one file at a time.

Example: Program to open multiple files simultaneously.

Output:

file manipulation clip image002 File Manipulation

File Modes

A file mode describes how a file is to be used; read, write, append and so on. When you associate a stream with a file, either by initializing a file stream object with a file name or by using the open( ) method, you can provide a second argument specifying the file mode which are as follows,

The second argument of open( ) method, file-mode, is of type int. A file-mode can be any of the several constants defined in the ios class. List of various file-modes is given below,

Name

Description

ios::in

Open file to read.

ios::out

Open file to write.

ios::app

All the data you write, is attached to the end of the file. It calls ios::out.

ios::ate

All the data you write, is attached to the end of the file. It does not call ios::out.

ios::trunc

Deletes all previous contents from the fie. (empties the file).

ios::nocreate

If the file does not exist, opening it with the open() function gets impossible.

ios::noreplace

If the file exists, trying to open it with the open() function, returns an error.

ios::binary

Opens a file in binary mode.

Closing a File

A file is closed by disconnecting it from the stream which is associated with it. The close() function accomplishes this task, its general form is,

For example, if a file called Master.txt is connected with an ofstream object fout then its connection with the stream can be terminated by the following statement,

fout.close() ;

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Daniel Memetic on

Leave a Reply

Related Posts

  • File Input/Output

    In C++, file input/output services are implemented through a component header file available C++ standard library. This header file is fstream.h . In C++, a file, at its lowest level, is interpreted simply as a sequence, or stream, of bytes. One aspect of the file I/O library manages the transfer of these bytes. At this [...]...


  • Jar File

    A jar file is a Java Archive. A jar file is a collection of class files. The jar file standard is based upon PKZip. In fact, you can open .jar files with a normal unzip program! A jar archive should contain a file called Manifest.mf which tells the Java Virtual Machine which class file contains [...]...


  • MKV File

    A MKV file is a container format that can be used to store audio and video formats and is considered a competing format to the MP4 and AVI container formats. MKV (Matroska) is an open source format based on EBML (Extensible Binary Meta Language), and includes a greater number of features than supported by other [...]...


  • How Windows File Swapping works

    The major advantage of the Microsoft Windows operating system over the previous Disk Operating System (DOS) is that one can open and use multiple programs at the same time; hence the term “Windows” (as in having several windows – or programs or applications – open at one and the same time). This led to the [...]...


  • How Vista File Sharing works

    File sharing is a necessity when there is a network. Whether it is the Internet or an intranet, people are sharing files all the time. It helps businesses function at an optimum level as people can share any information they want. File sharing makes files available for others to use. This is accomplished in many [...]...