Practical training c ++ language design-files and streams

Source: Internet
Author: User
Tags setf

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.