📄 chap24.lst
字号:
listing 3
class three_d {
public:
int x, y, z; // 3-d coordinates
three_d(int a, int b, int c) { x=a; y=b; z=c; }
} ;
listing 4
// Display X, Y, Z coordinates (three_d's inserter).
ostream &operator<<(ostream &stream, three_d obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
listing 5
#include <iostream>
using namespace std;
class three_d {
public:
int x, y, z; // 3-d coordinates
three_d(int a, int b, int c) { x=a; y=b; z=c; }
} ;
// Display X, Y, Z coordinates - three_d inserter.
ostream &operator<<(ostream &stream, three_d obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
int main()
{
three_d a(1, 2, 3), b(3, 4, 5), c(5, 6, 7);
cout << a << b << c;
return 0;
}
listing 6
// Limited version - don't use.
ostream &operator<<(ostream &stream, three_d obj)
{
cout << obj.x << ", ";
cout << obj.y << ", ";
cout << obj.z << "\n";
return stream; // return the stream
}
listing 7
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-d coordinates - - now private
public:
three_d(int a, int b, int c) { x=a; y=b; z=c; }
friend ostream &operator<<(ostream &stream, three_d obj);
} ;
// Display X, Y, Z coordinates - three_d inserter.
ostream &operator<<(ostream &stream, three_d obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
int main()
{
three_d a(1, 2, 3), b(3, 4, 5), c(5, 6, 7);
cout << a << b << c;
return 0;
}
listing 8
// Get three dimensional values - extractor.
istream &operator>>(istream &stream, three_d &obj)
{
cout <<
"Enter X Y Z values, separating each with a space: ";
stream >> obj.x >> obj.y >> obj.z;
return stream;
}
listing 9
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-d coordinates
public:
three_d(int a, int b, int c) {x=a; y=b; z=c;}
friend ostream &operator<<(ostream &stream, three_d obj);
friend istream &operator>>(istream &stream, three_d &obj);
} ;
// Display X, Y, Z coordinates - inserter.
ostream &operator<<(ostream &stream, three_d obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
// Get three dimensional values - extractor.
istream &operator>>(istream &stream, three_d &obj)
{
cout <<
"Enter X Y Z values, separating each with a space: ";
stream >> obj.x >> obj.y >> obj.z;
return stream;
}
int main()
{
three_d a(1, 2, 3);
cout << a;
cin >> a;
cout << a;
return 0;
}
listing 10
stream.setf(ios::showbase);
listing 11
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpos);
cout.setf(ios::scientific);
cout << 123 << " " << 123.23 << " ";
return 0;
}
listing 12
cout.setf(ios::scientific | ios::showpos);
listing 13
#include <iostream>
using namespace std;
void showflags (long f);
int main ()
{
long f;
f = cout.flags();
showflags(f);
cout.setf(ios::showpos);
cout.setf(ios::scientific);
f = cout.flags();
showflags(f);
cout.unsetf(ios::scientific);
f = cout.flags();
showflags(f);
return 0;
}
void showflags(long f)
{
long i;
for(i=0x4000; i; i = i >> 1)
if(i & f) cout << "1 ";
else cout << "0 ";
cout << "\n";
}
listing 14
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpos);
cout.setf(ios::scientific);
cout << 123 << " " << 123.23 << "\n";
cout.precision(2); // two digits after decimal point
cout.width(10); // in a field of ten characters
cout << 123 << " " << 123.23 << "\n";
cout.fill('#'); // fill using #
cout.width(10); // in a field of ten characters
cout << 123 << " " << 123.23;
return 0;
}
listing 15
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setiosflags(ios::fixed);
cout << setprecision(2) << 1000.243 << endl;
cout << setw(20) << "Hello there.";
return 0;
}
listing 16
#include <iostream>
#include <iomanip>
using namespace std;
main()
{
cout << setiosflags(ios::showpos);
cout << setiosflags(ios::scientific);
cout << 123 << " " << 123.23;
return 0;
}
listing 17
#include <iostream>
using namespace std;
int main()
{
char s[80];
cin >> ws >> s;
cout << s;
}
listing 18
#include <iostream>
#include <iomanip>
using namespace std;
ostream &setup(ostream &stream)
{
stream.setf(ios::left);
stream << setw(10) << setfill('$');
return stream;
}
int main()
{
cout << 10 << " " << setup << 10;
return 0;
}
listing 19
#include <iostream>
#include <iomanip>
using namespace std;
istream &prompt(istream &stream)
{
cin >> hex;
cout << "Enter number using hex format: ";
return stream;
}
int main()
{
int i;
cin >> prompt >> i;
cout << i;
return 0;
}
listing 20
ifstream in; // input
ofstream out; // output
fstream both; // input and output
listing 21
ofstream out;
out.open("test", ios::out);
listing 22
out.open("test"); // defaults to output and normal file
listing 23
if(!mystream) {
cout << "Cannot open file.\n";
// handle error
}
listing 24
ifstream mystream("myfile"); // open file for input
listing 25
if(!mystream.is_open()) {
cout << "File is not open.\n";
// ...
listing 26
mystream.close();
listing 27
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("test");
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
out << 10 << " " << 123.23 << "\n";
out << "This is a short text file.\n";
out.close();
return 0;
}
listing 28
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int i;
float f;
char str[80];
ifstream in("test");
if(!in) {
cout << "Cannot open file.\n";
return 1;
}
in >> i;
in >> f;
in >> str;
cout << i << " " << f << " " << "\n";
cout << str;
in.close();
return 0;
}
listing 29
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
char ch;
if(argc!=2) {
cout << "Usage: PR <filename>\n";
return 1;
}
ifstream in(argv[1], ios::in | ios::binary);
if(!in) {
cout << "Cannot open file.\n";
return 1;
}
while(in) { // in will be null when eof is reached
in.get(ch);
cout << ch;
}
in.close();
return 0;
}
listing 30
while(in.get(ch))
cout << ch;
listing 31
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char *p = "hello there\n\r\xfe\xff";
ofstream out("test", ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
while(*p) out.put(*p++);
out.close();
return 0;
}
listing 32
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n[5] = {1, 2, 3, 4, 5};
register int i;
ofstream out("test", ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
out.write((char *) &n, sizeof n);
out.close();
for(i=0; i<5; i++) // clear array
n[i] = 0;
ifstream in("test", ios::in | ios::binary);
in.read((char *) &n, sizeof n);
for(i=0; i<5; i++) // show values read from file
cout << n[i] << " ";
in.close();
return 0;
}
listing 33
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if(argc!=3) {
cout << "Usage: CHANGE <filename> <byte>\n";
return 1;
}
fstream out(argv[1], ios::in | ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
out.seekp(atoi(argv[2]), ios::beg);
out.put('X');
out.close();
return 0;
}
listing 34
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
char ch;
if(argc!=3) {
cout << "Usage: NAME <filename> <starting location>\n";
return 1;
}
ifstream in(argv[1], ios::in | ios::binary);
if(!in) {
cout << "Cannot open file.\n";
return 1;
}
in.seekg(atoi(argv[2]), ios::beg);
while(in.get(ch))
cout << ch;
in.close();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -