Use the following steps to open a file for input and to read from the file.
z/OS C/C++ provides overloads of the fstream
and ifstream constructors and their open() functions, which allow you to specify
file attributes such as lrecl and recfm.
ILE C++ provides overloads of the fstream
and istream constructors and their open functions, which allow you to specify
the ccsid of a file.
Construct an fstream or ifstream Object for Input
You can open a file for input in one of two ways:
#include <fstream.h>
int main(int argc, char *argv[]) {
fstream infile1;
ifstream infile2;
infile1.open(“myfile.dat”,ios::in);
infile2.open(“myfile.dat”);
// ...
}
#include <fstream.h>
int main(int argc, char *argv[]) {
fstream infile1(“myfile.dat”,ios::in);
ifstream infile2(“myfile.dat”);
// ...
}
The only difference between opening the file as an fstream or ifstream object is that, if you open the file as an fstream object, you must specify the input mode (ios::in). If you open it as an ifstream object, it is implicitly opened in input mode. The advantage of using ifstream rather than fstream to open an input file is that, if you attempt to apply the output operator to an ifstream object, this error will be caught during compilation. If you attempt to apply the output operator to an fstream object, the error is not caught during compilation, and may pass unnoticed at runtime.
The advantage of using fstream rather than ifstream is that you can use the same object for both input and output. For example:
// Using fstream to read from and write to a file
#include <fstream.h>
int main(int argc, char *argv[]) {
char q[40];
fstream myfile(“test.txt”,ios::in); // open the file for input
myfile >> q; // input from myfile into q
myfile.close(); // close the file
myfile.open(“test.txt”,ios::app); // reopen the file for output
myfile << q << endl; // output from q to myfile
myfile.close(); // close the file
return 0;
}
This example opens the same file first for input and later for output. It reads in a character string during input, and writes that character string to the end of the same file during output. Let's assume that the contents of the file test.txt before the program is run are:
barbers often shave
In this case, the file contains the following after the program is run:
barbers often shave barbers
Note that you can use the same fstream object to access different files in sequence. In the above example, myfile.open(“test.txt”,ios::app) could have read myfile.open(“test.out”,ios::app) and the program would still have compiled and run, although the end result would be that the first string of test.txt would be appended to test.out instead of to test.txt itself.
Read Input from a File
The statement myfile >> a reads input into a from the myfile stream. Input from an fstream or ifstream object resembles input from the standard input stream cin, in all respects except that the input is a file rather than standard input, and you use the fstream object name instead of cin. The two following programs produce the same output when provided with a given set of input. In the case of stdin.C, the input comes from the standard input device. In the case of filein.C, the input comes from the file file.in:
| stdin.C | filein.C |
|---|---|
#include <iostream.h>
int main(int argc, char *argv[]) {
int ia,ib,ic;
char ca[40],cb[40],cc[40];
// cin is predefined
cin >> ia >> ib >> ic
>> ca;
cin.getline(cb,sizeof(cb),'\n');
cin >> cc;
// no need to close cin
cout << ia << ca
<< ib << cb
<< ic << cc << endl;
return 0;
} |
#include <fstream.h>
int main(int argc, char *argv[]) {
int ia,ib,ic;
char ca[40],cb[40],cc[40];
fstream myfile(“file.in”,ios::in);
myfile >> ia >> ib >> ic
>> ca;
myfile.getline(cb,sizeof(cb),'\n');
myfile >> cc;
myfile.close();
cout << ia << ca
<< ib << cb
<< ic << cc << endl;
return 0;
} |
In both examples, the program reads the following, in sequence:
When you define an input operator for a class type, this input operator is available both to the predefined input stream cin and to any input streams you define, such as myfile in the above example.
All techniques for reading input from the standard input stream can also be used to read input from a file, providing your code is changed so that the cin object is replaced with the name of the fstream object associated with the input file.
Related Concepts
Related Tasks