码迷,mamicode.com
首页 > 编程语言 > 详细

Standard C++ Episode 7

时间:2015-08-21 07:03:05      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

六、C++的I/O流库
C:fopen/fclose/fread/fwrite/fprintf/fscanf/fseek/ftell...

C++:对基本的I/O操作做了类的封装,其功能没有任何差别,用法和C的I/O流也非常近似。

技术分享

 

 

七、格式化I/O

<</>>

 1 /*
 2  *格式化I/O练习
 3  */
 4 #include <iostream>
 5 #include <fstream>
 6 using namespace std;
 7 int main (void) {
 8     ofstream ofs ("format.txt");
 9     if (! ofs) {
10         perror ("打开文件失败");
11         return -1;
12     }
13     ofs << 1234 <<   << 56.78 <<   << "linux"
14         << \n;
15     ofs.close ();
16     ofs.open ("format.txt", ios::app);
17     if (! ofs) {
18         perror ("打开文件失败");
19         return -1;
20     }
21     ofs << "append_a_line\n";
22     ofs.close ();
23     ifstream ifs ("format.txt");
24     if (! ifs) {
25         perror ("打开文件失败");
26         return -1;
27     }
28     int i;
29     double d;
30     string s1, s2;
31     ifs >> i >> d >> s1 >> s2;
32     cout << i <<   << d <<   << s1 <<  
33         << s2 << endl;
34     ifs.close ();
35     return 0;
36 }

 

 

八、非格式化I/O

put/get

 1 /*
 2  *非格式化的I/O练习
 3  */
 4 #include <iostream>
 5 #include <fstream>
 6 using namespace std;
 7 int main (void) {
 8     ofstream ofs ("putget.txt");
 9     if (! ofs) {
10         perror ("打开文件失败");
11         return -1;
12     }
13     for (char c =  ; c <= ~; ++c)
14         if (! ofs.put (c)) {
15             perror ("写入文件失败");
16             return -1;
17         }
18     ofs.close ();
19     ifstream ifs ("putget.txt");
20     if (! ifs) {
21         perror ("打开文件失败");
22         return -1;
23     }
24     char c;
25     while ((c = ifs.get ()) != EOF)
26         cout << c;
27     cout << endl;
28     if (! ifs.eof ()) {
29         perror ("读取文件失败");
30         return -1;
31     }
32     ifs.close ();
33     return 0;
34 }

 

 

九、随机I/O
seekp/seekg

tellp/tellg

 1 /*
 2  * seekp/seekg练习
 3  * tellp/tellg练习
 4  * */
 5 #include <iostream>
 6 #include <fstream>
 7 using namespace std;
 8 int main (void) {
 9     fstream fs ("seek.txt", ios::in | ios::out);
10     if (! fs) {
11         perror ("打开文件失败");
12         return -1;
13     }
14     fs << "0123456789";
15     cout << fs.tellp () << endl;//tellp返回当前put操作位置
16     cout << fs.tellg () << endl;//tellg返回当前get操作位置
17     fs.seekp (-3, ios::cur);
18     fs << "XYZ";
19     fs.seekg (4, ios::beg);
20     int i;
21     fs >> i;
22     cout << i << endl;
23     cout << fs.tellg () << endl;
24     cout << fs.tellp () << endl;
25     fs.seekg (-6, ios::end);
26     fs << "ABC";
27     fs.close ();
28     return 0;
29 }

 

 

 

十、二进制I/O

read/write

 1 /*
 2  *read/write练习
 3  */
 4 #include <iostream>
 5 #include <fstream>
 6 #include <cstring>
 7 using namespace std;
 8 class Dog {
 9 public:
10     Dog (const string& name = "", int age = 0) :
11         m_age (age) {
12         strcpy (m_name, name.c_str ());
13     }
14     void print (void) const {
15         cout << m_name << "" << m_age << endl;
16     }
17 private:
18     char m_name[128];
19     int m_age;
20 };
21 int main (void) {
22     ofstream ofs ("dog.dat");
23     Dog dog ("小白", 25);
24     ofs.write ((char*)&dog, sizeof (dog));
25     ofs.close ();
26     ifstream ifs ("dog.dat");
27     Dog dog2;
28     ifs.read ((char*)&dog2, sizeof (dog2));
29     dog2.print ();
30     ifs.close ();
31     return 0;
32 }

 

 

 1 /*
 2  * 使用按位异或对文件加密和解密
 3  * */
 4 #include <iostream>
 5 #include <fstream>
 6 #include <stdexcept>
 7 #include <cstdlib>
 8 using namespace std;
 9 #define BUFSIZE (1024*10)
10 int _xor (const char* src, const char* dst,
11     unsigned char key) {
12     ifstream ifs (src, ios::binary);
13     if (! ifs) {
14         perror ("打开源文件失败");
15         return -1;
16     }
17     ofstream ofs (dst, ios::binary);
18     if (! ofs) {
19         perror ("打开目标文件失败");
20         return -1;
21     }
22     char* buf = NULL;
23     try {
24         buf = new char[BUFSIZE];
25     }
26     catch (bad_alloc& ex) {
27         cout << ex.what () << endl;
28         return -1;
29     }
30     while (ifs.read (buf, BUFSIZE)) {
31         for (size_t i = 0; i < BUFSIZE; ++i)
32             buf[i] ^= key;
33         if (! ofs.write (buf, BUFSIZE)) {
34             perror ("写入文件失败");
35             return -1;
36         }
37     }
38     if (! ifs.eof ()) {
39         perror ("读取文件失败");
40         return -1;
41     }
42     for (size_t i = 0; i < ifs.gcount (); ++i)
43         buf[i] ^= key;
44     if (! ofs.write (buf, ifs.gcount ())) {
45         perror ("写入文件失败");
46         return -1;
47     }
48     delete[] buf;
49     ofs.close ();
50     ifs.close ();
51     return 0;
52 }
53 int enc (const char* plain, const char* cipher) {
54     srand (time (NULL));
55     unsigned char key = rand () % 256;
56     if (_xor (plain, cipher, key) == -1)
57         return -1;
58     cout << "密钥:" << (unsigned int)key << endl;
59     return 0;
60 }
61 int dec (const char* cipher, const char* plain,
62     unsigned char key) {
63     return _xor (cipher, plain, key);
64 }
65 int main (int argc, char* argv[]) {
66     if (argc < 3) {
67         cerr << "用法:" << argv[0]
68             << " <明文文件> <密文文件>" << endl;
69         cerr << "用法:" << argv[0]
70             << " <密文文件> <明文文件> <密钥>"
71             << endl;
72         return -1;
73     }
74     if (argc < 4)
75         return enc (argv[1], argv[2]);
76     else
77         return dec (argv[1], argv[2],
78             atoi (argv[3]));
79     return 0;
80 }

 

 

 

十一、格式控制
in a;
printf ("%d%x\n", a, a)
cout << a << endl;
流函数,例如cout.precision(10)
流控制符,流控制符是对象,例如setprecision(10)。使用流控制符需要include <iomanip>
cout << hex << a;
cout << setw (10) << a;

 1 /*
 2  *格式控制练习
 3  */
 4 #include <iostream>
 5 #include <iomanip>
 6 #include <cmath>
 7 #include <fstream>
 8 #include <sstream>
 9 using namespace std;
10 int main (void) {
11     cout << sqrt (2) << endl;
12     cout.precision (10); // 10位有效数字
13     cout << sqrt (2) << endl;
14     cout << sqrt (2) * 100 << endl;
15     cout << setprecision (5) << sqrt (2) << endl
16         << sqrt (2) * 100 << endl;
17     cout << "当前精度:" << cout.precision ()
18         << endl;
19     cout << setprecision (2) << 1.24 <<   << 1.25
20         <<   << 1.26 << endl;
21     cout << showbase << hex << 127 << endl;
22     cout << oct << 127 << endl;
23     cout << dec << 127 << endl;
24     cout << noshowbase << hex << 127 << dec << endl;
25     cout << setw (12) << 127 << 721 << endl;
26     cout << setfill ($) << left << setw (12)
27         << 127 << endl;
28     cout.precision (10);
29     cout.setf (ios::scientific);
30     cout << sqrt (2) << endl;
31     cout.setf (ios::fixed);
32     cout << sqrt (2) << endl;
33     cout << 12.00 << endl;
34     cout << showpoint << 12.00 << endl;
35     cout << noshowpoint << 12.00 << endl;
36     ifstream ifs ("stream.txt");
37     ifs.unsetf (ios::skipws);
38     char c;
39     while (ifs >> c)
40         cout << c;
41     ifs.setf (ios::skipws);
42     ifs.clear (); // 复位
43     ifs.seekg (ios::beg);
44     while (ifs >> c)
45         cout << c;
46     ifs.close ();
47     cout << endl;
48     int i = 1234;
49     double d = 56.78;
50     string s = "tarena";
51     ostringstream oss;
52     oss << i <<   << d <<   << s;
53     string str = oss.str ();
54     cout << str << endl;
55     str = "hello 3.14 pai";
56     istringstream iss;
57     iss.str (str);
58     iss >> s >> d >> str;
59     cout << s <<   << d <<   << str << endl;
60     return 0;
61 }

 

Standard C++ Episode 7

标签:

原文地址:http://www.cnblogs.com/libig/p/4746707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!