VCL File Processing: C++ File Streaming |
|
Introduction |
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 an fstream 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. |
Saving a file consists of writing data to disk. To do this, first declare an instance of the ofstream class using one of its constructors from the following syntaxes: ofstream(const char* FileName, int FileMode); ofstream(); 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 what kind of operation you want to perform on the file. It can be one of the following:
Image you have a form with three edit boxes whose job is to get the first name, the last name, and the age of a student:
You can save its data using a SaveDialog as follows: //--------------------------------------------------------------------------- #include <vcl.h> #include <fstream> using namespace std; #pragma hdrstop #include "Exercise.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::btnSaveClick(TObject *Sender) { char FirstName[30], LastName[30]; int Age; strcpy(FirstName, edtFirstName->Text.c_str()); strcpy(LastName, edtLastName->Text.c_str()); Age = edtAge->Text.ToInt(); if( SaveDialog1->Execute() ) { ofstream Students(SaveDialog1->FileName.c_str(), ios::out); Students << FirstName << "\n" << LastName << "\n" << Age; } } The default constructor, ofstream(), can be used to create an empty stream if you do not yet have enough information about the file you intend to deal with and what type of operation you will perform. This constructor is used if you plan to call member methods to perform the desired file processing. void open(const char* FileName, int FileMode); 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. //--------------------------------------------------------------------------- void __fastcall TForm1::btnSaveClick(TObject *Sender) { char FirstName[30], LastName[30]; int Age; strcpy(FirstName, edtFirstName->Text.c_str()); strcpy(LastName, edtLastName->Text.c_str()); Age = edtAge->Text.ToInt(); if( SaveDialog1->Execute() ) { ofstream Students; Students.open(SaveDialog1->FileName.c_str()); Students << FirstName << "\n" << LastName << "\n" << Age; } } //--------------------------------------------------------------------------- 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. Like ofstream, 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. 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 the FileName value. After using the ifstream class, you can close it using the ifstream::close() method. Here is an example: |
//--------------------------------------------------------------------------- #include <vcl.h> #include <fstream> using namespace std; #pragma hdrstop #include "Exercise.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::btnSaveClick(TObject *Sender) { char FirstName[30], LastName[30]; int Age; strcpy(FirstName, edtFirstName->Text.c_str()); strcpy(LastName, edtLastName->Text.c_str()); Age = edtAge->Text.ToInt(); if( SaveDialog1->Execute() ) { ofstream Students; Students.open(SaveDialog1->FileName.c_str()); Students << FirstName << "\n" << LastName << "\n" << Age; } } //--------------------------------------------------------------------------- void __fastcall TForm1::btnOpenClick(TObject *Sender) { char FirstName[30], LastName[30]; int Age; if( OpenDialog1->Execute() ) { ifstream Students; Students.open(OpenDialog1->FileName.c_str()); Students >> FirstName >> LastName >> Age; Students.close(); edtFirstName->Text = FirstName; edtLastName->Text = LastName; edtAge->Text = Age; } } //---------------------------------------------------------------------------
Home | Copyright © 2005-2012, FunctionX, Inc. | |