Familiar with the usage of common classes and their member functions in stream class libraries, learned the control of standard input and output and their formats, and learned how to apply files, including binary files and text files.
Algorithm: None
Method: None
Program:
// Lab11_1.cpp
# Include <fstream>
Using namespace STD;
# Define D (a) T <# A <Endl;
Ofstream T ("output. Out ");
Void main ()
{D (INT I = 53 ;)
D (float F = 4700113.141593 ;)
Char * s = "Is there any more? ";
D (T. SETF (IOs: unitbuf );)
D (T. SETF (IOs: showbase );)
D (T. SETF (IOs: uppercase );)
D (T. SETF (IOs: showpos );)
D (T <I <Endl ;)
D (T. SETF (IOs: Hex, IOS: basefield );)
D (T <I <Endl ;)
D (T. unsetf (IOs: showbase );)
D (T. SETF (IOs: Oct, IOS: basefield );)
D (T <I <Endl ;)
D (T. unsetf (IOs: showbase );)
D (T. SETF (IOs: Dec, IOS: basefield );)
D (T. SETF (IOs: Left, IOS: adjustfield );)
D (T. Fill ('0 ');)
D (T <"fill CHAR:" <t. Fill () <Endl ;)
D (T. Width (8 );)
T <I <Endl;
D (T. SETF (IOs: Right, IOS: adjustfield );)
D (T. Width (8 );)
T <I <Endl;
D (T. SETF (IOs: Internal, IOS: adjustfield );)
D (T. Width (8 );)
T <I <Endl;
D (T <I <Endl;) // without width (10)
D (T. unsetf (IOs: showpos );)
D (T. SETF (IOs: showpoint );)
D (T <"prec =" <t. Precision () <Endl ;)
D (T. SETF (IOs: Scientific, IOS: floatfield );)
D (T <Endl <F <Endl ;)
D (T. SETF (IOs: fixed, IOS: floatfield );)
D (T <F <Endl ;)
D (T. SETF (0, IOS: floatfield);) // automatic
D (T <F <Endl ;)
D (T. Precision (16 );)
D (T <"prec =" <t. Precision () <Endl ;)
D (T <Endl <F <Endl ;)
D (T. SETF (IOs: Scientific, IOS: floatfield );)
D (T <Endl <F <Endl ;)
D (T. SETF (IOs: fixed, IOS: floatfield );)
D (T <F <Endl ;)
D (T. SETF (0, IOS: floatfield );)
D (T <F <Endl ;)
D (T. Width (8 );)
T <S <Endl;
D (T. Width (36 );)
T <S <Endl;
D (T. SETF (IOs: Left, IOS: adjustfield );)
D (T. Width (36 );)
T <S <Endl;
D (T. unsetf (IOs: showpoint );)
D (T. unsetf (IOs: unitbuf );)
}
// Lab11_1.cpp
# Include <iostream>
# Include <string>
Using namespace STD;
# Include <fstream>
Int main ()
{Char A1 [32], A2 [32];
Char STR [80];
Char * P;
Cout <"Create File" <Endl;
Cout <"Enter your file name (with a suffix)" <Endl;
Cin. Getline (A1, 32 );
P = A1;
Cout <"input file content:" <Endl;
Cin. Getline (STR, 80 );
Ofstream file (p, IOS: Binary );
If (! File)
{Cout <"cannot open this file! "<Endl;
Cout <"exit the program! "<Endl;
Exit (1 );}
Else
{File. Write (STR, 80 );
File. Close ();
}
Cout <"/n enter the name of the file you want to open (with a suffix)" <Endl;
Cin. Getline (A2, 32 );
P = a2;
Ifstream infile (p, IOS: Binary );
If (! Infile)
{Cout <"/n does not exist in this file" <Endl;
Cout <"exit the program! "<Endl;
Exit (1 );
}
Else
{String Q;
Infile> q;
Content in cout <"file" <p <"is:" <q;
File. Close ();
}
Cout <Endl;
Return 0;
}