📄 module11.lst
字号:
listing 1
// Demonstrate a custom inserter.
#include <iostream>
using namespace std;
class ThreeD {
public:
int x, y, z; // 3-D coordinates
ThreeD(int a, int b, int c) { x = a; y = b; z = c; }
};
// Display X, Y, Z coordinates - ThreeD inserter.
ostream &operator<<(ostream &stream, ThreeD obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
int main()
{
ThreeD a(1, 2, 3), b(3, 4, 5), c(5, 6, 7);
cout << a << b << c;
return 0;
}
listing 2
// Use a friend to overload <<.
#include <iostream>
using namespace std;
class ThreeD {
int x, y, z; // 3-D coordinates - now private
public:
ThreeD(int a, int b, int c) { x = a; y = b; z = c; }
friend ostream &operator<<(ostream &stream, ThreeD obj);
};
// Display X, Y, Z coordinates - ThreeD inserter.
ostream &operator<<(ostream &stream, ThreeD obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
int main()
{
ThreeD a(1, 2, 3), b(3, 4, 5), c(5, 6, 7);
cout << a << b << c;
return 0;
}
listing 3
// Demonstrate a custom extractor.
#include <iostream>
using namespace std;
class ThreeD {
int x, y, z; // 3-D coordinates
public:
ThreeD(int a, int b, int c) { x = a; y = b; z = c; }
friend ostream &operator<<(ostream &stream, ThreeD obj);
friend istream &operator>>(istream &stream, ThreeD &obj);
} ;
// Display X, Y, Z coordinates - ThreeD inserter.
ostream &operator<<(ostream &stream, ThreeD obj)
{
stream << obj.x << ", ";
stream << obj.y << ", ";
stream << obj.z << "\n";
return stream; // return the stream
}
// Get three dimensional values - ThreeD extractor.
istream &operator>>(istream &stream, ThreeD &obj)
{
cout << "Enter X,Y,Z values: ";
stream >> obj.x >> obj.y >> obj.z;
return stream;
}
int main()
{
ThreeD a(1, 2, 3);
cout << a;
cin >> a;
cout << a;
return 0;
}
listing 4
// Use setf().
#include <iostream>
using namespace std;
int main()
{
// Turn on showpos and scientific flags.
cout.setf(ios::showpos);
cout.setf(ios::scientific);
cout << 123 << " " << 123.23 << " ";
return 0;
}
listing 5
// Demonstrate flags() and unsetf().
#include <iostream>
using namespace std;
int main()
{
ios::fmtflags f;
f = cout.flags();
if(f & ios::showpos)
cout << "showpos is set for cout.\n";
else
cout << "showpos is cleared for cout.\n";
cout << "\nSetting showpos for cout.\n";
cout.setf(ios::showpos);
f = cout.flags();
if(f & ios::showpos)
cout << "showpos is set for cout.\n";
else
cout << "showpos is cleared for cout.\n";
cout << "\nClearing showpos for cout.\n";
cout.unsetf(ios::showpos);
f = cout.flags();
if(f & ios::showpos)
cout << "showpos is set for cout.\n";
else
cout << "showpos is cleared for cout.\n";
return 0;
}
listing 6
// Demonstrate width(), precision(), and fill().
#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 10 characters
cout << 123 << " ";
cout.width(10); // set width to 10
cout << 123.23 << "\n";
cout.fill('#'); // fill using #
cout.width(10); // in a field of 10 characters
cout << 123 << " ";
cout.width(10); // set width to 10
cout << 123.23;
return 0;
}
listing 7
// Demonstrate an I/O manipulator.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setprecision(2) << 1000.243 << endl;
cout << setw(20) << "Hello there.";
return 0;
}
listing 8
// Use setiosflags().
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setiosflags(ios::showpos) <<
setiosflags(ios::scientific) <<
123 << " " << 123.23;
return 0;
}
listing 9
// Skip leading whitespace.
#include <iostream>
using namespace std;
int main()
{
char s[80];
cin >> ws >> s;
cout << s;
return 0;
}
listing 10
// Create an output manipulator.
#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 11
// Create an input manipulator.
#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 12
// Write to file.
#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.";
out.close();
return 0;
}
listing 13
// Read from file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
int i;
float f;
char str[80];
ifstream in("test");
if(!in) {
cout << "Cannot open file.\n";
return 1;
}
in >> i;
in >> f;
in >> ch;
in >> str;
cout << i << " " << f << " " << ch << "\n";
cout << str;
in.close();
return 0;
}
listing 14
// Display a file using get().
#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 false when eof is reached
in.get(ch);
if(in) cout << ch;
}
in.close();
return 0;
}
listing 15
// Use put() to write to a file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char *p = "hello there\n";
ofstream out("test", ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
// Write characters until the null-terminator is reached.
while(*p) out.put(*p++);
out.close();
return 0;
}
listing 16
// Use read() and write().
#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);
if(!in) {
cout << "Cannot open file.\n";
return 1;
}
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 17
// Use get() to read a string that contains spaces.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char str[80];
cout << "Enter your name: ";
cin.get(str, 79);
cout << str << '\n';
return 0;
}
listing 18
/*
Project 11-1
Create a file comparision utility.
*/
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
register int i;
int numread;
unsigned char buf1[1024], buf2[1024];
if(argc!=3) {
cout << "Usage: compfiles <file1> <file2>\n";
return 1;
}
ifstream f1(argv[1], ios::in | ios::binary);
if(!f1) {
cout << "Cannot open first file.\n";
return 1;
}
ifstream f2(argv[2], ios::in | ios::binary);
if(!f2) {
cout << "Cannot open second file.\n";
return 1;
}
cout << "Comparing files...\n";
do {
f1.read((char *) buf1, sizeof buf1);
f2.read((char *) buf2, sizeof buf2);
if(f1.gcount() != f2.gcount()) {
cout << "Files are of differing sizes.\n";
f1.close();
f2.close();
return 0;
}
// compare contents of buffers
for(i=0; i<f1.gcount(); i++)
if(buf1[i] != buf2[i]) {
cout << "Files differ.\n";
f1.close();
f2.close();
return 0;
}
} while(!f1.eof() && !f2.eof());
cout << "Files are the same.\n";
f1.close();
f2.close();
return 0;
}
listing 19
// Demonstrate random access.
#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 20
// Display a file from a given starting point.
#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;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -