Standard C++ Episode 7

六、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 }

?

轉載于:https://www.cnblogs.com/libig/p/4746707.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375614.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375614.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375614.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在Android設備與Mac電腦之間傳輸文件

不同于Windows和Linux&#xff0c;Android設備連接到Mac電腦上是看不見掛載的目錄的&#xff0c;既然看不到了Android設備的掛載目錄&#xff0c;如何在Android設備與Mac電腦之間傳輸文件呢&#xff1f; 原來Android官方提供了傳輸文件的工具&#xff01;訪問www.android.com/f…

mysql語句在node.js中的寫法

總結一下mysql語句在node.js中的各種寫法&#xff0c;參考了npm網站mysql模塊給的實例。 查詢 select //1 db.query(select * from tuanshang_users where user_id < 10,function(err,results,fields){//if(err) throw err;console.log( results );if(!!results.length){con…

jqPlot圖表插件學習之折線圖-散點圖-series屬性

一、準備工作 首先我們需要到官網下載所需的文件&#xff1a; 官網下載&#xff08;筆者選擇的是jquery.jqplot.1.0.8r1250.zip這個版本&#xff09; 然后讀者需要根據自己的情況新建一個項目并且按照如下的方式加載對應的js和css&#xff08;因為筆者在VS2012環境下新建的&…

node.js基礎:數據存儲

無服務器的數據存儲 內存存儲 var http require(http); var count 0; //服務器訪問次數存儲在內存中 http.createServer(function(req,res){res.write(hello count);res.end(); }).listen(3000);    基于文件的存儲 node.js中主要用fs文件系統模塊來管理文件的存儲。 文件…

CUDA 6.5 VS2013 Win7:創建CUDA項目

運行環境&#xff1a; Win7VS2013CUDA6.5 1.創建win32空項目 2.右鍵項目解決方案-->生成項目依賴項-->生成自定義 3.右鍵項目解決方案-->屬性-->配置屬性-->常規-->平臺工具集 配置屬性-->VC目錄-->包含目錄&#xff0c;添加 $(CUDA_INC_PATH) 連接器-…

c/c++編碼規范(2)--作用域

2. 作用域 靜止使用class類型的靜態或全局變量。 6. 命名約定 6.1. 函數名&#xff0c;變量名&#xff0c;文件名要有描述性&#xff0c;少用縮寫。 6.2. 文件命名 6.2.1. 文件名要全部用小寫。可使用“_”或"-"&#xff0c;遵從項目規范&#xff0c;沒有規范&#x…

subversion svnserver服務啟動與配置

svnserve 是一個輕量級的服務&#xff0c; 使用自定義的協議通過TCP/IP與客戶端通訊。 客戶端通過由 svn:// 或者 svnssh:// 開始的URL訪問svnserve服務器。 啟動服務器 端口監控&#xff08;inetd&#xff09;模式 如果你打算用端口監控來啟動處理客戶的訪問請求的進程&#x…

mongodb地理空間索引原理閱讀摘要

http://www.cnblogs.com/taoweiji/p/3710495.html 具體原理在上面 簡單概述&#xff0c;&#xff08;x,y&#xff09;經緯度坐標&#xff0c;通過geohash的方式&#xff0c;通過N次方塊四分割生成一個坐標碼&#xff0c;然后用坐標碼進行BTREE的索引建立轉載于:https://www.cnb…

angular 頁面加載時可以調用 函數處理

轉載于 作者:海底蒼鷹地址:http://blog.51yip.com/jsjquery/1599.html 我希望頁面加載的時候&#xff0c;我能馬上處理頁面的數據&#xff0c;如請求API .... 所以這樣設置 在某個頁面的控制器中 監聽頁面load phonecatControllers.controller(registerctr, [$scope, $routePa…

刪除排序數組中的重復項

給定一個排序數組&#xff0c;你需要在原地刪除重復出現的元素&#xff0c;使得每個元素只出現一次&#xff0c;返回移除后數組的新長度。 不要使用額外的數組空間&#xff0c;你必須在原地修改輸入數組并在使用 O(1) 額外空間的條件下完成。 示例 1: 給定數組 nums [1,1,2…

android 處理鼠標滾輪事件 【轉】

android處理鼠標滾輪事件&#xff0c;并不是如下函數&#xff1a; 1&#xff09; public boolean onKeyDown(int keyCode, KeyEvent event) 2) public boolean dispatchKeyEvent(KeyEvent event) 3) public boolean onTouchEvent(MotionEvent event) 而是如下函數 …

ASP.NET數據報表之柱狀圖 ------工作日志

#region 柱形色調 /// <summary> /// 柱形色調 /// </summary> private string[] myColor new string[] { "DarkGreen", "DimGray", "DodgerBlue", "Orchid", //Peru "Orange", "Orchid", &q…

接口安全--簽名驗證

為防止第三方冒充客戶端請求服務器&#xff0c;可以采用參數簽名驗證的方法&#xff1a; 將請求參數中的各個鍵值對按照key的字符串順序升序排列&#xff08;大小寫敏感&#xff09;&#xff0c;把key和value拼成一串之后最后加上密鑰&#xff0c;組成key1value1key2value2PRIV…

Runtime類

Runtime類也在java.lang包中&#xff0c;這個類沒有提供構造器&#xff0c;但是提供的卻非靜態方法&#xff0c;而是在方法中提供了一個靜態方法來返回當前進程的Runtime實例&#xff0c;采用的單例設計模式。 其作用&#xff1a;可以對當前java程序進程進行操作、打開本機程序…

Spring MVC 返回NULL時客戶端用$.getJSON的問題

如果Spring MVC返回是NULL&#xff0c;那么客戶端的$.getJSON就不會觸發&#xff1b; 20170419補充 后臺的輸出為&#xff1a; DEBUG [org.springframework.web.servlet.DispatcherServlet] - Null ModelAndView returned to DispatcherServlet with name springMVC: assuming …

duilib設置滾動條自動滾動到底

控件屬性中添加 vscrollbar"true" autovscroll"true"分別是啟用豎向滾動條&#xff0c;是否隨輸入豎向滾動

MVC,MVP 和 MVVM 的圖示

復雜的軟件必須有清晰合理的架構&#xff0c;否則無法開發和維護。 MVC&#xff08;Model-View-Controller&#xff09;是最常見的軟件架構之一&#xff0c;業界有著廣泛應用。它本身很容易理解&#xff0c;但是要講清楚&#xff0c;它與衍生的 MVP 和 MVVM 架構的區別就不容易…

Java JDBC學習實戰(二): 管理結果集

在我的上一篇博客《Java JDBC學習實戰&#xff08;一&#xff09;&#xff1a; JDBC的基本操作》中&#xff0c;簡要介紹了jdbc開發的基本流程&#xff0c;并詳細介紹了Statement和PreparedStatement的使用&#xff1a;利用這兩個API可以執行SQL語句&#xff0c;完成基本的CURD…

error: storage size of ‘threads’ isn’t known

出錯的代碼行&#xff1a; pthread_t threads[NUM_THREADS];原因&#xff1a; NUM_THREADS 無值 原先&#xff1a; #define NUM_THREADS修改為 #define NUM_THREADS 5

android之相機開發

http://blog.csdn.net/jason0539/article/details/10125017 android之相機開發 分類&#xff1a; android 基礎知識2013-08-20 22:32 9774人閱讀 評論(2) 收藏 舉報Android在android中應用相機功能&#xff0c;一般有兩種&#xff1a;一種是直接調用系統相機&#xff0c;一種自…