Handling files in C ++

Write operations in files function

The header file fstream.h defines the ifstream, ostream and fstream classes for read, write and read / write operations on files respectively. To work with files we must create objects of these classes according to the operations we want to perform. We start with the writing operations, for which we basically declare an object of the ofstream class, then we use the member function open to open the file, we write in the file the data that are necessary using the insertion operator and finally we close the file by means of the close member function, this process is illustrated in our first program, archiv01.cpp.

// **********************************************
// archiv01.cpp
// Demonstrates basic writing on file
// © 1999, Jaime Virgilio Gómez Negrete
// **********************************************

#include

int main ()
{
ofstream file; // object of the ofstream class

file.open (“data.txt”);

file << “First line of text” << endl;
<< << Second line of text << << endl;
<< << Last line of text << << endl;

file.close ();
return 0;
}

In the program an object of the ofstream class called file has been created, then the open member function is used to open the specified tree in the text string that is inside the parenthesis of the function. We can invoke the class constructor function in such a way that the file can also be opened using the following instruction:

ofstream file (“data.txt”); // construction of ofstream

Naturally, when using the constructor function it is no longer necessary to use the open member function, this way of opening a file is preferred because the code is more compact and easier to read. In the same way that output manipulators are used to modify the on-screen display of program data, it is still possible to use these manipulators when writing data to a file as demonstrated by the program archiv02.cpp, note that a constructor is used to create and open the file named Data.txt:

// **********************************************
// archiv02.cpp
// Demonstrates the use of manipulators
// © 1999, Jaime Virgilio Gómez Negrete
// **********************************************

#include
#include
#include

int main ()
{
ofstream file (“Datos.txt”); // constructor of ofstream
int number;

cout << “Enter a number:” << endl;     cin >> number;
file << “The value entered in base 10 is:” << number << endl;

file << resetiosflags (ios :: dec);
<< setiosflags (ios :: oct) file;
file << “on octal basis is:” << number << endl;

file << resetiosflags (ios :: oct);
<< setiosflags (ios :: hex) file;
file << “and in hexadecimal basis is:” << number << endl;
file << setiosflags (ios :: uppercase | ios :: showbase);
file << “using the uppercase and showbase handlers”
<< “the value is:” << number << endl;

file << resetiosflags (ios :: uppercase | ios :: showbase);
file << resetiosflags (ios :: hex);
file << setiosflags (ios :: showpos | ios :: showpoint | ios :: fixed);
file << “Using the showpos handlers,” << “showpoint and fixed:” << (float) number << endl;

file << resetiosflags (ios :: showpos | ios :: showpoint | ios :: fixed);
file << “Finally the value is” << number << endl;

file.close ();

return 0;
}

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!