전체 페이지뷰

2013년 2월 6일 수요일

namespace , File I/O Statements

• You can define a class and place the definition of the class and the implementation of its member functions in separate files. You can then compile the class separately from any program that uses it, and you can use this same class in any number of different programs.
• A namespace is a collection of name definitions, such as class definitions and variable declarations.
• There are three ways to use a name from a namespace: by making all the names in the namespace available with a using directive, by making the single name available with a using declaration for the one name, or by qualifying the name with the name of the namespace and the scope resolution operator.
• You place a definition in a namespace by placing the definition in a namespace grouping for that namespace.
• The unnamed namespace can be used to make a name definition local to a compila- tion unit.

namespace S1
{
namespace S2
{
void sample( )
{
. . .
} .
.
.
} //S2
}//S1

Unnamed Namespace
You can use the unnamed namespace to make a definition local to a compilation unit. Each compilation unit has one unnamed namespace. All the identifiers defined in the unnamed namespace are local to the compilation unit. You place a definition in the unnamed namespace by placing the definition in a namespace grouping with no namespace name, as shown next:
namespace
{
Definition_1
Definition_2
. . .
Definition_Last
}
You can use any name in the unnamed namespace without qualifiers anyplace in the compilation unit.

Summary of File I/O Statements
In this example the input comes from a file with the name infile.txt, and the output goes to a file with the name outfile.txt.
■ Place the following include directives in your program file:
#include
#include
#include
For file I/O For cout
For exit
Add the following using directives (or something similar):
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl; //if endl is used.
■ Choose a stream name for the input stream and declare it to be a variable of type ifstream. Choose a stream name for the output file and declare it to be of type ofstream. For example,
ifstream inStream;
ofstream outStream;
■ Connect each stream to a file using the member function open with the external file name as an argument. Remember to use the member function fail to test that the call to open was successful:
inStream.open("infile.txt");
if (inStream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1); }
outStream.open("outfile.txt");
if (outStream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1); }
■ Use the stream inStream to get input from the file infile.txt just like you use cin to get input from the keyboard. For example,
inStream >> someVariable >> someOtherVariable;
■ Use the stream outStream to send output to the file outfile.txt just like you use
cout to send output to the screen. For example, outStream << "someVariable = "
<< someVariable << endl;
■ Close the streams using the function close:
inStream.close( );
outStream.close( );

• A stream of type ifstream can be connected to a file with a call to the member function open. Your program can then take input from that file.
• A stream of type ofstream can be connected to a file with a call to the member function open. Your program can then send output to that file.
• You should use the member function fail to check whether a call to open was successful.
• Stream member functions, such as width, setf, and precision, can be used to format output. These output functions work the same for the stream cout, which is connected to the screen, and for output streams connected to files.
• A function may have formal parameters of a stream type, but they must be call-by- reference parameters. They cannot be call-by-value parameters. The type ifstream can be used for an input-file stream, and the type ofstream can be used for an output-file stream. (See the next summary point for other type possibilities.)
• If you use istream (spelled without the "f") as the type for an input stream param- eter, then the argument corresponding to that formal parameter can be either the stream cin or an input-file stream of type ifstream (spelled with the "f"). If you use ostream (spelled without the "f") as the type for an output stream parameter, then the argument corresponding to that formal parameter can be either the stream cout, the stream cerr, or an output-file stream of type ofstream (spelled with the "f").
• The member function eof can be used to test when a program has reached the end of an input file.
• The same input and output functions you use to manipulate files can be used to manipulate strings through the stringstream class. The stringstream class pro- vides a simple way to create a string from variables of other data types or to read variables of other data types from a string.

댓글 없음:

댓글 쓰기