【C/C++学院】0826-文件重定向/键盘输入流/屏幕输出流/字符串输入输出/文件读写简单操作/字符文件读写二进制与文本差别/get与getline挖掘数据/二进制与文本差别/随机位置/多线程初级

文件重定向



[java]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. void main()  
  5. {  
  6.     char str[30] = { 0 };  
  7.     cin >> str;  
  8.     cout << str;  
  9.     system(str);  
  10.   
  11.     cerr << "error for you";  
  12.   
  13.     cin.get();  
  14.     cin.get();  
  15. }  

键盘输入流

[java]  view plain copy
  1. #include<iostream>  
  2. #include <stdlib.h>  
  3.   
  4. using namespace std;  
  5. void main1()  
  6. {  
  7.     char ch;  
  8.     cin >> ch;  
  9.     cout << (char)(ch-32);//cout重载了很多数据类型  
  10.   
  11.     cin.get();  
  12.     cin.get();  
  13. }  
  14.   
  15. void  main2()  
  16. {  
  17.     char ch = 0;  
  18.     while ((ch=cin.get())!='\t')//复合表达式  
  19.     {  
  20.         cin.get();  
  21.         cout.put(ch);  
  22.     }  
  23. }  
  24.   
  25. void  main3()  
  26. {  
  27.     char str[10] = {0};  
  28.     cin.getline(str, 10);//限定长度  
  29.   
  30.     cout << str;  
  31.   
  32.     system("pause");  
  33. }  
  34.   
  35. void  main()//引用的方式  
  36. {  
  37.     char ch = 0;  
  38.     while (  ch!= '\t')//复合表达式  
  39.     {  
  40.         cin.get(ch);//等价于ch=cin.get  
  41.         cin.get();  
  42.         cout.put(ch);  
  43.     }  
  44. }  

屏幕输出流/实数整数输出/格式控制

[java]  view plain copy
  1. #include<iostream>  
  2. #include<iomanip>//控制输出流  
  3.   
  4. using namespace std;  
  5.   
  6. void main1()  
  7. {  
  8.     cout.put('A').put('B').put('C');  
  9.   
  10.     char  str[] = "123456789abcdefg";  
  11.   
  12.     cout.write(str, 10);//最大输出10个字符,不包含/0  
  13.   
  14.     cin.get();  
  15. }  
  16.   
  17. void main2()  
  18. {  
  19.     //dec,oct,hex都是格式控制符  
  20.     int num = 01070;  
  21.     cout << num << endl;//默认十进制  
  22.   
  23.     cout << hex;//十六进制强制标识,endl结束不了  
  24.     cout << num << num << "\n" << endl;  
  25.     cout << oct;//八进制强制标识,endl结束不了  
  26.     cout << num << num<<"\n"<<endl;  
  27.     cout << dec;  
  28.     cout << num << endl;//默认十进制  
  29.     cout << num << endl;//默认十进制  
  30.       
  31.     cin.get();  
  32. }  
  33.   
  34. void  main3()  
  35. {  
  36.     double db = 1.98123178387127838718732;  
  37.     //cout <<db << endl;//小数点后面六位  
  38.     cout << setprecision(25) << db;//小数点显示精确度  
  39.       
  40.     cin.get();  
  41. }  
  42.   
  43. void  main4()  
  44. {  
  45.     cout.width (40); //设定显示的宽度  
  46.     cout.fill('&');//填充字符  
  47.     cout << "hello world"<<endl;  
  48.       
  49.     cin.get();  
  50. }  
  51.   
  52.   
  53. void  main()  
  54. {  
  55.     //字符串输出de特点  
  56.     cout.width(40); //设定显示的宽度  
  57.     cout.fill('&');//填充字符  
  58.     cout.setf(ios::left);//输出的内容左对齐  
  59.     cout << "hello world" << endl;  
  60.   
  61.     cout.width(20); //设定显示的宽度,如果实际长度helloworld超过了2,按照实际长度输出  
  62.     cout.fill('*');//填充字符  
  63.     cout.setf(ios::right,ios::left);//清除左对齐,置右对齐  
  64.   
  65.     cout << "hello world" << endl;  
  66.     cin.get();  
  67. }  
  68.   
  69.   
  70. void main6()  
  71. {  
  72.     int num1;  
  73.     cin.setf(ios::hex, ios::basefield);//设置输入为十六进制  
  74.     cin >> num1;  
  75.     cout.setf(ios::hex, ios::basefield);//设置十六进制  
  76.     cout << num1;  
  77.   
  78.     int num2;  
  79.     cin.setf(ios::dec, ios::basefield);//设置输入为十进制  
  80.     cin >> num2;  
  81.     cout.setf(ios::dec, ios::basefield);  
  82.     cout << num2;  
  83.   
  84.     int num3;  
  85.     cin.setf(ios::oct, ios::basefield);//设置输入为8进制  
  86.     cin >> num3;  
  87.     cout.setf(ios::oct, ios::basefield);  
  88.     cout << num3;  
  89.   
  90.   
  91.     cin.get();  
  92.     cin.get();  
  93.     cin.get();  
  94.     cin.get();  
  95.     cin.get();  
  96. }  
  97.   
  98. void main7()  
  99. {  
  100.     double  db = 100 / 7.0;  
  101.     cout.setf(ios::fixed | ios::showpoint);//定点  
  102.     for (int i = 1; i < 10; i++)  
  103.     {  
  104.         cout.precision(i);//控制小数点多少位  
  105.         cout << db << endl;  
  106.     }  
  107.     cout << db<<endl;  
  108.     //db = 1000000000000000000000.0;  
  109.     cout.setf(ios::scientific, ios::fixed | ios::showpoint);  
  110.     //实数,根据方便自动选择指数或者定点小数  
  111.     cout << db << endl;  
  112.       
  113.     cin.get();  
  114. }  
  115.   
  116.   
  117. void  main8()  
  118. {  
  119.     const int num = 8848;  
  120.     cout << setw(10) << setfill('*') << setiosflags(ios::left) << num << endl;  
  121.     cout << setw(10) << setfill('*') << setiosflags(ios::right) << num << endl;  
  122.     cout << resetiosflags(ios::right) << setw(10) << setbase(8) << setfill('X') << setiosflags(ios::left) << num << endl;  
  123.     //resetiosflags 清除历史遗迹  
  124.     //setw宽度  
  125.     //setbase基数,决定进制  
  126.   
  127.     cin.get();  
  128. }  

字符串输入输出

[java]  view plain copy
  1. #include<iostream>  
  2. #include<sstream>  
  3. #include<string>  
  4.   
  5. using namespace std;  
  6.   
  7. struct MyStruct  
  8. {  
  9.     string str1, str2, str3;  
  10.     double db;  
  11.     int num;  
  12.     char ch;  
  13. };  
  14.   
  15. void main1()  
  16. {  
  17.     string  mystring("china  google  microsoft 12.9 123 A");  
  18.     //string.replace '#'  ' '  
  19.     MyStruct  struct1;  
  20.   
  21.     istringstream input(mystring);//创建一个字符串扫描流  
  22.     input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;  
  23.     cout << struct1.str1 << endl;  
  24.     cout << struct1.str2<< endl;  
  25.     cout << struct1.str3 << endl;  
  26.     cout << struct1.db << endl;  
  27.     cout << struct1.num << endl;  
  28.     cout << struct1.ch << endl;  
  29.   
  30.     cin.get();  
  31. }  
  32.   
  33. void main2()  
  34. {  
  35.     char   mystring[50]="china#123#A";    
  36.   
  37.     for (char *p = mystring; *p != '\0'; p++)  
  38.     {  
  39.         if (*p == '#')  
  40.         {  
  41.             *p = ' ';  
  42.         }  
  43.     }  
  44.       
  45.     istringstream input(mystring);//创建一个字符串扫描流  
  46.   
  47.     string str;  
  48.     int num;  
  49.     char ch;  
  50.     input >> str >> num >> ch;  
  51.   
  52.     cout <<str << endl;  
  53.     cout <<num << endl;  
  54.     cout << ch << endl;  
  55.   
  56.     cin.get();  
  57. }  
  58.   
  59.   
  60. void main3()  
  61. {  
  62.     ostringstream  MYOUT;  
  63.     char str[100] = { 0 };  
  64.     //ostringstream MYOUT(str,sizeof(str));  
  65.   
  66.     char str1[50] = "a1234567b";  
  67.   
  68.     MYOUT << "a1234b" << 123 << 234.89 << 'h' << str1 << endl;  
  69.     //cout << MYOUT.str();  
  70.     //cout <<str;  
  71.   
  72.     cin.get();  
  73. }  
  74.   
  75. #include<strstream>  
  76. void main233()  
  77. {  
  78.     char str[100] = { 0 };  
  79.     ostrstream MYOUT(str, sizeof(str));//初始化,ostrstrean给char  
  80.     //ostringstream   
  81.   
  82.     char str1[50] = "a1234567b";  
  83.     MYOUT << "a1234b"  << str1 << ends;  
  84.     cout << MYOUT.str() << endl;  
  85.     std::cout<<str;     
  86.   
  87.     cin.get();  
  88. }  

字符串流

[java]  view plain copy
  1. #define _CRT_SECURE_NO_WARNINGS  
  2. #include <iostream>  
  3. #include <sstream>  
  4. #include <string>  
  5. #include <stdlib.h>  
  6.   
  7. using namespace std;  
  8. void mainA()  
  9. {  
  10.     stringstream mystr;//字符串进行输入,  
  11.     mystr.put('X').put('Y');//连个字符输入  
  12.     mystr << "ZXCV";//字符串输入  
  13.     cout << mystr.str();  
  14.   
  15.     string str = mystr.str();//定义字符串接受值  
  16.       
  17.     char ch;    //从字符串内部读取一个字符  
  18.     mystr >> ch;  
  19.     cout << "\n";  
  20.     cout.put(ch);  
  21.   
  22.   
  23.     cout << "\n";  
  24.     cout << mystr.str();  
  25.     std::cin.get();  
  26. }  
  27.   
  28. void main()  
  29. {  
  30.     stringstream mystr;//sprintf功能  
  31.     char cmd1[30] = { 0 };  
  32.     char cmd2[30] = { 0 };  
  33.     cin.getline(cmd1, 30).getline(cmd2, 30);//读取  
  34.     mystr << cmd1 << "&" << cmd2;//字符打印  
  35.     string str = mystr.str();//定义字符串接受值  
  36.     system(str.c_str());  
  37.   
  38.     char cstr[50] = { 0 };//默认的字符串  
  39.     strcpy(cstr, str.c_str());  
  40.     cout << cstr << endl;  
  41.     for (char *p = cstr; *p != '\0'; p++)  
  42.     {  
  43.         if (*p == '&')  
  44.         {  
  45.             *p = ' ';  
  46.         }  
  47.     }  
  48.     char newcmd1[30] = { 0 };  
  49.     char newcmd2[30] = { 0 };  
  50.     stringstream  newstr(cstr);//sscanf的功能  
  51.     newstr >> newcmd1 >> newcmd2;  
  52.     cout << newcmd1<<"\n"<<newcmd2<< endl;  
  53.   
  54.   
  55.     system("pause");  
  56. }  

文件读写简单操作/文件读写按行读写扫描读写

[java]  view plain copy
  1. #include <iostream>  
  2. #include<fstream>  
  3.   
  4. using namespace std;  
  5.   
  6. void main1()  
  7. {  
  8.    ofstream fout;//ofstream.输出文件  
  9.    fout.open("C:\\1.txt");//打开文件  
  10.    fout << "1234abcdef";//写入文件  
  11.    fout.close();  
  12. }  
  13.   
  14. void main2()  
  15. {  
  16.     ifstream fin("C:\\1.txt");//创建读取文件的流  
  17.     char str[50] = { 0 };  
  18.     fin >> str;//读取  
  19.     fin.close();  
  20.     cout << str;  
  21.     cin.get();  
  22. }  
  23.   
  24. void main3()  
  25. {  
  26.     //按照行来读取  
  27.     ifstream fin("C:\\1.txt");  
  28.     for (int i = 0; i < 4; i++)  
  29.     {  
  30.         char str[50] = { 0 };  
  31.         fin.getline(str, 50);  
  32.         cout << str << endl;  
  33.     }  
  34.     fin.close();  
  35.     cin.get();  
  36. }  
  37.   
  38. void main4()  
  39. {  
  40.     ofstream fout;//ofstream.输出文件  
  41.     fout.open("C:\\2.txt");//打开文件  
  42.     fout << "锄禾日当午"<<endl;//写入文件  
  43.     fout << "地雷买下土" << endl;//写入文件  
  44.     fout << "谭胜来跳舞" << endl;//写入文件  
  45.     fout << "炸成250" << endl;//写入文件  
  46.     fout.close();  
  47. }  
  48.   
  49. void  main5()  
  50. {  
  51.     fstream fio("C:\\3.txt", ios::in | ios::out);  
  52.     fio << "锄禾日当午" << endl;//写入文件  
  53.     fio << "地雷买下土" << endl;//写入文件  
  54.     fio << "谭胜来跳舞" << endl;//写入文件  
  55.     fio << "炸成250" << endl;//写入文件  
  56.     fio.close();  
  57.     {  
  58.         fstream fio("C:\\3.txt", ios::in | ios::out);  
  59.         for (int i = 0; i < 4; i++)  
  60.         {  
  61.             char str[50] = { 0 };  
  62.             fio.getline(str, 50);  
  63.             cout << str << endl;  
  64.         }  
  65.         fio.close();  
  66.     }  
  67.       
  68.   
  69.     cin.get();  
  70. }  
  71.   
  72. void  main6()  
  73. {  
  74.     fstream fio("C:\\4.txt", ios::in | ios::out);  
  75.     fio << "锄禾日当午" << endl;//写入文件  
  76.     fio << "地雷买下土" << endl;//写入文件  
  77.     fio << "谭胜来跳舞" << endl;//写入文件  
  78.     fio << "炸成250" << endl;//写入文件  
  79.       
  80.     fio.seekg(ios::beg);//文件指针  ios::beg开始  
  81.   
  82.     for (int i = 0; i < 4; i++)  
  83.     {  
  84.             char str[50] = { 0 };  
  85.             fio.getline(str, 50);  
  86.             cout << str << endl;  
  87.     }  
  88.    fio.close();   
  89.   
  90.     cin.get();  
  91. }  
  92.   
  93. //写入文件,不需要转换为字符串  
  94. //读取的时候,不需要吧字符串转换为其他类型的操作  
  95. void  main7()  
  96. {  
  97.     ofstream fout;  
  98.     fout.open("C:\\X.txt");  
  99.     fout << "ABC" << " " << 123 << " " << 'ch'<<endl;//打印到文件  
  100.     fout.close();  
  101.     ifstream fin("C:\\X.txt");//创建读取文件的流  
  102.     char str[10] = { 0 };//读取字符串  
  103.     int num = 0;  
  104.     char ch = '\0';  
  105.     fin >> str >> num >> ch;  
  106.     std::cout << str << "\n" << num << "\n" << ch;      
  107.   
  108.     std::cin.get();  
  109. }  
  110.   
  111. //读写一个字符  
  112. //文本与二进制存储差别不一样  
  113. void main123213()  
  114. {  
  115.     ifstream fin("C:\\4.txt");//创建读取文件的流  
  116.     ofstream fout("C:\\40.txt");  
  117.     if (!fin || !fout)  
  118.     {  
  119.         std::cout << "文件打开失败";  
  120.         return;  
  121.     }  
  122.     std::cout << "文件拷贝开始\n";  
  123.     char ch=0;  
  124.     while (fout && fin.get(ch))//引用的方式读取到一个字符  
  125.     {  
  126.         fout.put(ch);//写入一个字节  
  127.   
  128.     }  
  129.     fin.close();  
  130.     fout.close();  
  131.   
  132.     std::cout << "文件拷贝完成";  
  133.   
  134.     cin.get();  
  135. }  
  136.   
  137. void  main10()  
  138. {  
  139.     ofstream fout("C:\\40.txt",ios::app);//追加  
  140.     fout << "天下英雄,谭胜第一";  
  141.     fout.close();  
  142.       
  143.     cin.get();  
  144. }  

iosQT

使用TightVNC软件,远程连接mac机器进行开发。

TightVNC

一款用于windows操作系统的应用软件,是一款远程控制软件。

使用教程

1、需要在被控方电脑上打开TvnServer,记住端口的相应信息;

2、如果需要密码验证,在主密码处设置好密码即可,TightVNC的界面也是非常友好的。

字符文件读写二进制与文本差别

文本文件

1  2 3

00000001  00000010   00000011

二进制

123

01111011

get与getline挖掘数据

[java]  view plain copy
  1. #include<iostream>  
  2. #include <stdlib.h>  
  3. using namespace std;  
  4.   
  5. void main1()  
  6. {  
  7.     {  
  8.         char buf[80];  
  9.         cin.get(buf, 80'#');//提取一段文本,最大长度为80,遇到#结束  
  10.         std::cout << buf;  
  11.     }  
  12.   
  13.     system("pause");  
  14.     std::cin.get();  
  15.     std::cin.get();  
  16. }  
  17.   
  18. void main2()  
  19. {  
  20.     {  
  21.         char buf[80];  
  22.         cin.get(buf, 80);//以回车结束,最大长度为80  
  23.         cin >> buf;//cin无法区分空格  
  24.         std::cout << buf;  
  25.     }  
  26.       
  27.     system("pause");  
  28.     std::cin.get();  
  29.     std::cin.get();  
  30. }  
  31.   
  32. void main3()  
  33. {  
  34.     {  
  35.         char buf[8];  
  36.         cin.get(buf, 8,'n');//如果记录回车,空格,可以以任何字符  
  37.         //cin >> buf;//cin无法区分空格  
  38.         std::cout << buf;  
  39.     }  
  40.   
  41.     system("pause");  
  42.     std::cin.get();  
  43.     std::cin.get();  
  44. }  
  45.   
  46. void main4()  
  47. {  
  48.     {  
  49.         char buf[80];  
  50.         cin.get(buf, 40'n');//如果记录回车,空格,可以以任何字符  
  51.         std::cout << buf<<"\n";//n意味着结束,后面不会读取  
  52.         cin.get(buf, 40'n');  
  53.         std::cout << buf << "\n";  
  54.     }  
  55.   
  56.     system("pause");  
  57.     std::cin.get();  
  58.     std::cin.get();  
  59. }  
  60.   
  61. void main()  
  62. {  
  63.     char buf[80];  
  64.     //默认/n,可以设定,可以反复读取  
  65.     cin.getline(buf, 80,',');//逐行读取  
  66.     std::cout << buf << "\n";  
  67.     cin.getline(buf, 80,',');//逐行读取  
  68.     std::cout << buf << "\n";  
  69.     cin.getline(buf, 80',');//逐行读取  
  70.     std::cout << buf << "\n";  
  71.     cin.getline(buf, 80'\n');//逐行读取  
  72.     std::cout << buf << "\n";  
  73.   
  74.     //cin.get(buf, 80,'x');//一次性读取,以X为结束  
  75.     //std::cout << buf << "\n";  
  76.   
  77.     system("pause");  
  78.     std::cin.get();  
  79.     std::cin.get();  
  80. }  

二进制与文本差别

[java]  view plain copy
  1. #include<iostream>  
  2. #include<fstream>  
  3. #include<string>  
  4. using namespace std;  
  5.   
  6. struct MyStruct  
  7. {  
  8.     char *p = "北京是帝都";  
  9.     int num = 20;  
  10.     double db = 10.98;  
  11.     char ch = 'a';  
  12. };  
  13.   
  14. void main1()  
  15. {  
  16.     ofstream fout("C:\\wen.txt",ios::out);  
  17.     ifstream fin("C:\\wen.txt");  
  18.     std::cout << sizeof(MyStruct) << std::endl;  
  19.     MyStruct my1;  
  20.     fout << my1.p <<" "<< my1.num <<" "<< my1.db <<" "<< my1.ch << "\n";  
  21.     fout.close();  
  22.     char str[100] = { 0 };  
  23.     fin.getline(str, 1000);//提取  
  24.     std::cout << str << std::endl;  
  25.     fin.close();  
  26.   
  27.     cin.get();  
  28. }  
  29.   
  30. void main()  
  31. {  
  32.     MyStruct my1;  
  33.     my1.p = "chuheridangwu";  
  34.     ofstream fout("C:\\bin.bin", ios::binary);  
  35.     fout.write((char *)&my1, sizeof(my1));//  
  36.     //第一个参数是要写入文件的内存的首地址,  
  37.     //第二个参数是长度  
  38.     fout.close();  
  39.     ifstream fin("C:\\bin.bin", ios::binary);  
  40.     MyStruct newmy1;  
  41.     fin.read((char*)&newmy1, sizeof(newmy1));  
  42.     //保存文件读取到内存,内存首地址  
  43.     //长度  
  44.     std::cout << newmy1.p << std::endl;  
  45.     fin.close();  
  46.   
  47.     std::cin.get();  
  48. }  

二进制文件读写

字节的二进制

[java]  view plain copy
  1. #include<iostream>  
  2. #include<fstream>  
  3. #include<string>  
  4. using namespace std;  
  5.   
  6. //按照字节的方式读写二进制,  
  7. //文件加密解密需要字节的方式  
  8.   
  9. void main1()  
  10. {     
  11.     ifstream fin("C:\\write.exe", ios::binary);  
  12.     ofstream fout("C:\\newwrite.exe", ios::binary);  
  13.     if (!fin || !fout)  
  14.     {  
  15.         std::cout << "文件打开失败";  
  16.         return;  
  17.     }  
  18.     std::cout << "文件拷贝开始\n";  
  19.     char ch = 0;  
  20.     while (fout && fin.get(ch))//引用的方式读取到一个字符  
  21.     {  
  22.         fout.put(ch);//写入一个字节  
  23.     }  
  24.     fin.close();  
  25.     fout.close();  
  26.   
  27.     std::cout << "文件拷贝完成";  
  28.   
  29.     std::cin.get();  
  30. }  

二进制内存文件的拷贝

[java]  view plain copy
  1. #include<iostream>  
  2. #include<fstream>  
  3. #include<string>  
  4.   
  5. using namespace std;  
  6.   
  7. struct MyStruct  
  8. {  
  9.     int i;  
  10.     double db;  
  11. };  
  12.   
  13. void main()  
  14. {  
  15.     ofstream fout("C:\\big.txt", ios::binary);  
  16.     MyStruct ss[5] = { { 11.1 }, { 22.2 }, { 33.3 }, { 44.4 }, { 55.5 } };  
  17.     fout.write((char *)ss, sizeof(MyStruct)* 5);  
  18.     fout.close();  
  19.   
  20.     ifstream fin("C:\\big.txt", ios::binary);  
  21.     MyStruct *p = new MyStruct[5];//动态分配内存  
  22.     fin.read((char *)p, sizeof(MyStruct)* 5);  
  23.     fin.close();  
  24.   
  25.     for (int i = 0; i < 5; i++)  
  26.     {  
  27.         std::cout << p[i].i << " " << p[i].db << std::endl;  
  28.     }  
  29.   
  30.     cin.get();  
  31. }  

随机位置文本二进制读写

随机文本文本读写

[java]  view plain copy
  1. #include<iostream>  
  2. #include <fstream>  
  3. using namespace std;  
  4.   
  5. void main1()//随机位置读取  
  6. {  
  7.     ofstream fout("p.txt");  
  8.     fout << "1234567890abcdefghijklmn";  
  9.     fout.close();  
  10.     ifstream fin("p.txt");  
  11.     //fin.seekg(9, ios::beg);//从开始,往前9个字符  
  12.     fin.seekg(-9, ios::end);//从尾部,倒数9个字符  
  13.     char ch;  
  14.     while (fin.get(ch))  
  15.     {  
  16.         cout << ch;  
  17.     }  
  18.     fin.close();  
  19.   
  20.     cin.get();  
  21. }  
  22.   
  23.   
  24. void main2()  
  25. {  
  26.     ofstream fout("px.txt");  
  27.     fout << "1234567890abcdefghijklmn";  
  28.     fout.close();  
  29.   
  30.     ofstream Fout("px.txt", ios::in | ios::out);  
  31.     char str[] = "ABCDEFG";  
  32.     Fout.seekp(0, ios::end);//随机位置进行读写  
  33.     long size = Fout.tellp();//当前位置距离begin有多少个字节,尾部可以获取文件大小  
  34.   
  35.     cout << size<<endl;  
  36.     Fout << str;  
  37.     Fout.close();  
  38.   
  39.     ifstream fin("px.txt");  
  40.     char ch;  
  41.     while (fin.get(ch))  
  42.     {  
  43.         cout << ch;  
  44.     }  
  45.   
  46.     fin.close();      
  47.   
  48.     cin.get();  
  49. }  

随机二进制文本读写

[java]  view plain copy
  1. #include<iostream>  
  2. #include <fstream>  
  3. using namespace std;  
  4.   
  5. void main1a()  
  6. {  
  7.     double db[10] = { 1.12.23.34.45.56.67.78.89.910.1 };  
  8.     ofstream  fout("N.txt", ios::binary);  
  9.     fout.write((char *)db, 80);  
  10.     fout.close();  
  11.     double *p = new double[10];  
  12.     ifstream fin("N.txt", ios::binary);  
  13.   
  14.     fin.read((char *)p, 80);  
  15.     fin.close();  
  16.     for (int i = 0; i < 10; i++)  
  17.     {  
  18.         std::cout << p[i] << endl;  
  19.     }  
  20.       
  21.     std::cin.get();  
  22. }  
  23.   
  24. void main2b()//随机位置读取  
  25. {  
  26.     double db[10] = { 1.12.23.34.45.56.67.78.89.910.1 };  
  27.     ofstream  fout("N.txt", ios::binary);  
  28.     fout.write((char *)db, 80);  
  29.     fout.close();  
  30.     double *p = new double[5];  
  31.     ifstream fin("N.txt", ios::binary);  
  32.     fin.seekg(-40, ios::end);//读写  
  33.     fin.read((char *)p, 40);  
  34.     fin.close();  
  35.     for (int i = 0; i < 5; i++)  
  36.     {  
  37.         std::cout << p[i] << endl;  
  38.     }  
  39.   
  40.     std::cin.get();  
  41. }  
  42.   
  43.   
  44. void main()  
  45. {  
  46.     //先读取  
  47.     double *p = new double[10];  
  48.     ifstream fin("N.txt", ios::binary);  
  49.     fin.read((char *)p, 80);  
  50.     fin.close();  
  51.     for (int i = 0; i < 10; i++)  
  52.     {  
  53.         std::cout << p[i] << endl;  
  54.     }  
  55.   
  56.     //随机写入  
  57.     double db[] = { 100.9200.8300.7400.6500.5 };  
  58.     //ios::in | ios::out不再清零文件,否则会清零  
  59.     ofstream  fout("N.txt", ios::binary|ios::in | ios::out);  
  60.     //fout.seekp(40, ios::beg);  
  61.     fout.seekp(0, ios::end);//写入  
  62.     fout.write((char *)db, 40);  
  63.     fout.close();  
  64.   
  65.     //再次读取  
  66.     {  
  67.         double *p = new double[20];  
  68.         ifstream fin("N.txt", ios::binary);  
  69.         fin.read((char *)p, 160);  
  70.         fin.close();  
  71.         for (int i = 0; i < 20; i++)  
  72.         {  
  73.             std::cout << p[i] << endl;  
  74.         }  
  75.     }  
  76.   
  77.     std::cin.get();  
  78. }  

多线程初级

[java]  view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. #include <string>  
  4.   
  5. using namespace std;  
  6.   
  7. void helloworld()  
  8. {  
  9.     std::cout << "你好天朝" << endl;  
  10. }  
  11.   
  12. void helloworldA()  
  13. {  
  14.     std::cout << "你好天朝A" << endl;  
  15. }  
  16.   
  17. void helloworldB()  
  18. {  
  19.     std::cout << "你好天朝B" << endl;  
  20. }  
  21.   
  22. void main1()  
  23. {  
  24.     std::thread t1(helloworld);//线程顺序执行  
  25.     std::thread t2(helloworldA);  
  26.     std::thread t3(helloworldB);  
  27.       
  28.     cin.get();  
  29. }  


[java]  view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. #include <string>  
  4. #include<windows.h>  
  5.   
  6. using namespace std;  
  7.   
  8. void run(int num)  
  9. {  
  10.     Sleep(1000);  
  11.     std::cout << "你好天朝"<< num<< endl;  
  12. }  
  13.   
  14. void main2()  
  15. {  
  16.     //thread t[10];  
  17.     thread *p[10];  
  18.     for (int i = 0; i < 10;i++)  
  19.     {  
  20.         p[i]=new thread(run, i);//循环创建线程  
  21.         p[i]->join();//等待  
  22.         //p[i]->detach();//脱离当前主线程自由执行,乱序  
  23.       
  24.     }  
  25.   
  26.     cin.get();  
  27. }  

线程之间通信以及锁定

[java]  view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. #include <string>  
  4. #include<windows.h>  
  5.   
  6.   
  7. using namespace std;  
  8.   
  9. //两个线程并行访问一个变量  
  10.   
  11. int g_num = 20;//找到或者找不到的标识  
  12.   
  13. void goA(int num)  
  14. {  
  15.     for (int i = 0; i < 15; i++)  
  16.     {  
  17.         Sleep(1000);  
  18.         if (i == 6)  
  19.         {  
  20.             g_num = 5;  
  21.         }  
  22.         if (g_num ==5)  
  23.         {  
  24.               
  25.           std::cout << "线程" << num << "结束   \n";  
  26.             return;  
  27.         }  
  28.         std::cout << "线程" << num <<"   "<< g_num << endl;  
  29.     }  
  30.       
  31. }  
  32.   
  33. void goB(int num)  
  34. {  
  35.     for (int i = 0; i <150; i++)  
  36.     {  
  37.         Sleep(1000);  
  38.         if (g_num == 5)  
  39.         {  
  40.             std::cout << "线程" << num << "结束   \n";  
  41.             return;  
  42.         }  
  43.         std::cout << "线程" << num << "   " << g_num << endl;  
  44.     }     
  45. }  
  46.   
  47. void main3()  
  48. {  
  49.     thread t1(goA, 1);  
  50.     thread t2(goB, 2);  
  51.     t1.join();  
  52.     t2.join();  
  53.   
  54.     std::cin.get();  
  55. }  

线程锁定

[java]  view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. #include <string>  
  4. #include<windows.h>  
  5. #include<mutex>  
  6.   
  7. using namespace std;  
  8.   
  9. //两个线程并行访问一个变量  
  10.   
  11. int g_num = 20;//找到或者找不到的标识  
  12. mutex g_mutex;  
  13.   
  14. void goA(int num)  
  15. {  
  16.     g_mutex.lock();//你访问的变量,在你访问期间,别人访问不了  
  17.       
  18.     for (int i = 0; i < 15; i++)  
  19.     {  
  20.         Sleep(300);  
  21.         g_num = 10;  
  22.         std::cout << "线程" << num << "   " << g_num << endl;  
  23.     }  
  24.     g_mutex.unlock();  
  25. }  
  26.   
  27. void goB(int num)  
  28. {  
  29.     for (int i = 0; i < 15; i++)  
  30.     {  
  31.         Sleep(500);  
  32.         g_num = 11;  
  33.         std::cout << "线程" << num << "   " << g_num << endl;  
  34.     }  
  35. }  
  36.   
  37. void main()  
  38. {  
  39.     thread t1(goA, 1);  
  40.     thread t2(goB, 2);  
  41.     t1.join();  
  42.     t2.join();  
  43.       
  44.     std::cin.get();  
  45. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值