kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Tuesday, October 4, 2011

Source: http://www.functionx.com/cpp/articles/filestreaming.htm


C++ File Streaming





File Processing in C++

Overview

File processing in C++ is performed using the fstream class. Unlike the FILE structure, fstream is a complete C++ class with constructors, a destructor and overloaded operators.

To perform file processing, you can declare an instance of anfstream object. If you do not yet know the name of the file you want to process, you can use the default constructor.

Unlike the FILE structure, the fstream class provides two distinct classes for file processing. One is used to write to a file and the other is used to read from a file.

Initializing a File

When processing a file, you will typically specify the type of operation you want to perform. The operation is specified using what is referred to as a file mode. It can be one of the following:

Mode
Description
ios::app
If FileName is a new file, data is written to it.
If FileName already exists and contains data, then it is opened, the compiler goes to the end of the file and adds the new data to it.
ios::ate
If FileName is a new file, data is written to it and subsequently added to the end of the file.
If FileName  already exists and contains data, then it is opened and data is written in the current position.
ios::in
If FileName is a new file, then it gets created fine as an empty file.
If FileName already exists, then it is opened and its content is made available for processing
ios::out
If FileName is a new file, then it gets created fine as an empty file. Once/Since it gets created empty, you can write data to it.
If FileName already exists, then it is opened, its content is destroyed, and the file becomes as new. Therefore you can create new data to write to it. Then, if you save the file, which is the main purpose of this mode, the new content is saved it.*This operation is typically used when you want to save a file
ios::trunc
If FileName already exists, its content is destroyed and the file becomes as new
ios::nocreate
If FileName is a new file, the operation fails because it cannot create a new file.
If FileName already exists, then it is opened and its content is made available for processing
ios::noreplace
If FileName is a new file, then it gets created fine.
If FileName already exists and you try to open it, this operation would fail because it cannot create a file of the same name in the same location.

Saving a File

One of the operations you can perform on a file consists of saving it, which is equivalent to storing its value(s) to a medium. To save a file, you can first declare an instance of theofstream class using one of its constructors from the following syntaxes:
ofstream();

ofstream(const char* FileName, int FileMode);
The default constructor allows you to initiate file processing without giving details. If you decide to use the default constructor, you can then call one of the methods we will see to perform the necessary operation. 
The ofstream(const char* FileName, int FileMode) constructor provides a complete mechanism for creating a file. It does this with the help of its two arguments. The first argument, FileName, is a string that specifies the name of the file that needs to be saved. The second argument, FileMode, specifies the kind of operation you want to perform on the file. It can be one of the modes we listed above.
Once you have decided what you want to do with a file, you use the << operator to save each value. Here is an example:
#include <fstream>

#include <iostream>

using namespace std;



int main()

{

    char FirstName[30], LastName[30];

    int Age;

    char FileName[20];



    cout << "Enter First Name: ";

    cin >> FirstName;

    cout << "Enter Last Name:  ";

    cin >> LastName;

    cout << "Enter Age:        ";

    cin >> Age;



    cout << "\nEnter the name of the file you want to create: ";

    cin >> FileName;

    ofstream Students(FileName, ios::out);

    Students << FirstName << "\n" << LastName << "\n" << Age;



    cout << "\n\n";

    return 0;

}
If you had used the default constructor to declare an ofstream variable, you can call theopen() method to actually process the file. The syntax of the open method is:
void open(const char* FileName, int Mode, int nProt = filebuf::openprot );
This method behaves exactly like, and uses the same arguments as, the constructor we described above. The first argument represents the name of the file you are dealing with and the FileMode argument follows the modes of the above table.

Because the fstream class in this case is declared as ofstream, the compiler is aware that you want to save a file (in reality, the use of ofstream means that you want to write to a file, in other words you want the FileMode with a value of ios::out), you can use the second constructor with just the FileName as argument or you can call the open() method with only the name of the file.
After using a file, you should close it. This is taken care by using the ofstream::close()method whose syntax is:
void close();


Opening a File

Besides saving, another operation you can perform consists of opening an already existing file to have access to its contents. To do this, C++ provides the ifstream class. Likeofstream, the ifstream class provides various constructors you can use, two of which are particularly important. If you have enough information about the file you want to open, you can use the following constructor:
ifstream(const char* FileName, int FileMode);
The first argument of the constructor, FileName, is a constant string that represents the file that you want to open. The FileMode argument is a natural number that follows the table of modes as we described above.
If necessary, you can also declare an empty instance of the ifstream class using the default constructor:
ifstream();
After declaring this constructor, you can use the ifstream::open() method to formally open the intended file. The syntax of the open() method is:
open(const char* FileName, int FileMode);
This method uses the same arguments as the above constructor. By default, when declaring an instance of the ifstream class, it is assumed that you want to open a file; that is, you want to use the FileMode attribute with a value of ios::in. Therefore, the second argument is already set to ios::in value. This allows you to call the open() method with just theFileName value.

After using the ifstream class, you can close it using the ifstream::close() method. Here is an example:
#include <fstream>

#include <iostream>

using namespace std;



int main()

{

    char FirstName[30], LastName[30];

    int Age;

    char FileName[20];

/*

    cout << "Enter First Name: ";

    cin >> FirstName;

    cout << "Enter Last Name:  ";

    cin >> LastName;

    cout << "Enter Age:        ";

    cin >> Age;



    cout << "\nEnter the name of the file you want to create: ";

    cin >> FileName;

    ofstream Students(FileName, ios::out);

    Students << FirstName << "\n" << LastName << "\n" << Age;

*/

    cout << "Enter the name of the file you want to open: ";

    cin >> FileName;

    ifstream Students(FileName);

    Students >> FirstName >> LastName >> Age;



        cout << "\nFirst Name: " << FirstName;

    cout << "\nLast Name:  " << LastName;

    cout << "\nEnter Age:  " << Age;



    cout << "\n\n";

    return 0;

}


0 comments:

Post a Comment

domain, domain name, premium domain name for sales

Popular Posts