C++ File Streaming | |
|
#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