文件重定向
- #include<iostream>
- using namespace std;
- void main()
- {
- char str[30] = { 0 };
- cin >> str;
- cout << str;
- system(str);
- cerr << "error for you";
- cin.get();
- cin.get();
- }
键盘输入流
- #include<iostream>
- #include <stdlib.h>
- using namespace std;
- void main1()
- {
- char ch;
- cin >> ch;
- cout << (char)(ch-32);//cout重载了很多数据类型
- cin.get();
- cin.get();
- }
- void main2()
- {
- char ch = 0;
- while ((ch=cin.get())!='\t')//复合表达式
- {
- cin.get();
- cout.put(ch);
- }
- }
- void main3()
- {
- char str[10] = {0};
- cin.getline(str, 10);//限定长度
- cout << str;
- system("pause");
- }
- void main()//引用的方式
- {
- char ch = 0;
- while ( ch!= '\t')//复合表达式
- {
- cin.get(ch);//等价于ch=cin.get
- cin.get();
- cout.put(ch);
- }
- }
屏幕输出流/实数整数输出/格式控制
- #include<iostream>
- #include<iomanip>//控制输出流
- using namespace std;
- void main1()
- {
- cout.put('A').put('B').put('C');
- char str[] = "123456789abcdefg";
- cout.write(str, 10);//最大输出10个字符,不包含/0
- cin.get();
- }
- void main2()
- {
- //dec,oct,hex都是格式控制符
- int num = 01070;
- cout << num << endl;//默认十进制
- cout << hex;//十六进制强制标识,endl结束不了
- cout << num << num << "\n" << endl;
- cout << oct;//八进制强制标识,endl结束不了
- cout << num << num<<"\n"<<endl;
- cout << dec;
- cout << num << endl;//默认十进制
- cout << num << endl;//默认十进制
- cin.get();
- }
- void main3()
- {
- double db = 1.98123178387127838718732;
- //cout <<db << endl;//小数点后面六位
- cout << setprecision(25) << db;//小数点显示精确度
- cin.get();
- }
- void main4()
- {
- cout.width (40); //设定显示的宽度
- cout.fill('&');//填充字符
- cout << "hello world"<<endl;
- cin.get();
- }
- void main()
- {
- //字符串输出de特点
- cout.width(40); //设定显示的宽度
- cout.fill('&');//填充字符
- cout.setf(ios::left);//输出的内容左对齐
- cout << "hello world" << endl;
- cout.width(20); //设定显示的宽度,如果实际长度helloworld超过了2,按照实际长度输出
- cout.fill('*');//填充字符
- cout.setf(ios::right,ios::left);//清除左对齐,置右对齐
- cout << "hello world" << endl;
- cin.get();
- }
- void main6()
- {
- int num1;
- cin.setf(ios::hex, ios::basefield);//设置输入为十六进制
- cin >> num1;
- cout.setf(ios::hex, ios::basefield);//设置十六进制
- cout << num1;
- int num2;
- cin.setf(ios::dec, ios::basefield);//设置输入为十进制
- cin >> num2;
- cout.setf(ios::dec, ios::basefield);
- cout << num2;
- int num3;
- cin.setf(ios::oct, ios::basefield);//设置输入为8进制
- cin >> num3;
- cout.setf(ios::oct, ios::basefield);
- cout << num3;
- cin.get();
- cin.get();
- cin.get();
- cin.get();
- cin.get();
- }
- void main7()
- {
- double db = 100 / 7.0;
- cout.setf(ios::fixed | ios::showpoint);//定点
- for (int i = 1; i < 10; i++)
- {
- cout.precision(i);//控制小数点多少位
- cout << db << endl;
- }
- cout << db<<endl;
- //db = 1000000000000000000000.0;
- cout.setf(ios::scientific, ios::fixed | ios::showpoint);
- //实数,根据方便自动选择指数或者定点小数
- cout << db << endl;
- cin.get();
- }
- void main8()
- {
- const int num = 8848;
- cout << setw(10) << setfill('*') << setiosflags(ios::left) << num << endl;
- cout << setw(10) << setfill('*') << setiosflags(ios::right) << num << endl;
- cout << resetiosflags(ios::right) << setw(10) << setbase(8) << setfill('X') << setiosflags(ios::left) << num << endl;
- //resetiosflags 清除历史遗迹
- //setw宽度
- //setbase基数,决定进制
- cin.get();
- }
字符串输入输出
- #include<iostream>
- #include<sstream>
- #include<string>
- using namespace std;
- struct MyStruct
- {
- string str1, str2, str3;
- double db;
- int num;
- char ch;
- };
- void main1()
- {
- string mystring("china google microsoft 12.9 123 A");
- //string.replace '#' ' '
- MyStruct struct1;
- istringstream input(mystring);//创建一个字符串扫描流
- input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;
- cout << struct1.str1 << endl;
- cout << struct1.str2<< endl;
- cout << struct1.str3 << endl;
- cout << struct1.db << endl;
- cout << struct1.num << endl;
- cout << struct1.ch << endl;
- cin.get();
- }
- void main2()
- {
- char mystring[50]="china#123#A";
- for (char *p = mystring; *p != '\0'; p++)
- {
- if (*p == '#')
- {
- *p = ' ';
- }
- }
- istringstream input(mystring);//创建一个字符串扫描流
- string str;
- int num;
- char ch;
- input >> str >> num >> ch;
- cout <<str << endl;
- cout <<num << endl;
- cout << ch << endl;
- cin.get();
- }
- void main3()
- {
- ostringstream MYOUT;
- char str[100] = { 0 };
- //ostringstream MYOUT(str,sizeof(str));
- char str1[50] = "a1234567b";
- MYOUT << "a1234b" << 123 << 234.89 << 'h' << str1 << endl;
- //cout << MYOUT.str();
- //cout <<str;
- cin.get();
- }
- #include<strstream>
- void main233()
- {
- char str[100] = { 0 };
- ostrstream MYOUT(str, sizeof(str));//初始化,ostrstrean给char
- //ostringstream
- char str1[50] = "a1234567b";
- MYOUT << "a1234b" << str1 << ends;
- cout << MYOUT.str() << endl;
- std::cout<<str;
- cin.get();
- }
字符串流
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- void mainA()
- {
- stringstream mystr;//字符串进行输入,
- mystr.put('X').put('Y');//连个字符输入
- mystr << "ZXCV";//字符串输入
- cout << mystr.str();
- string str = mystr.str();//定义字符串接受值
- char ch; //从字符串内部读取一个字符
- mystr >> ch;
- cout << "\n";
- cout.put(ch);
- cout << "\n";
- cout << mystr.str();
- std::cin.get();
- }
- void main()
- {
- stringstream mystr;//sprintf功能
- char cmd1[30] = { 0 };
- char cmd2[30] = { 0 };
- cin.getline(cmd1, 30).getline(cmd2, 30);//读取
- mystr << cmd1 << "&" << cmd2;//字符打印
- string str = mystr.str();//定义字符串接受值
- system(str.c_str());
- char cstr[50] = { 0 };//默认的字符串
- strcpy(cstr, str.c_str());
- cout << cstr << endl;
- for (char *p = cstr; *p != '\0'; p++)
- {
- if (*p == '&')
- {
- *p = ' ';
- }
- }
- char newcmd1[30] = { 0 };
- char newcmd2[30] = { 0 };
- stringstream newstr(cstr);//sscanf的功能
- newstr >> newcmd1 >> newcmd2;
- cout << newcmd1<<"\n"<<newcmd2<< endl;
- system("pause");
- }
文件读写简单操作/文件读写按行读写扫描读写
- #include <iostream>
- #include<fstream>
- using namespace std;
- void main1()
- {
- ofstream fout;//ofstream.输出文件
- fout.open("C:\\1.txt");//打开文件
- fout << "1234abcdef";//写入文件
- fout.close();
- }
- void main2()
- {
- ifstream fin("C:\\1.txt");//创建读取文件的流
- char str[50] = { 0 };
- fin >> str;//读取
- fin.close();
- cout << str;
- cin.get();
- }
- void main3()
- {
- //按照行来读取
- ifstream fin("C:\\1.txt");
- for (int i = 0; i < 4; i++)
- {
- char str[50] = { 0 };
- fin.getline(str, 50);
- cout << str << endl;
- }
- fin.close();
- cin.get();
- }
- void main4()
- {
- ofstream fout;//ofstream.输出文件
- fout.open("C:\\2.txt");//打开文件
- fout << "锄禾日当午"<<endl;//写入文件
- fout << "地雷买下土" << endl;//写入文件
- fout << "谭胜来跳舞" << endl;//写入文件
- fout << "炸成250" << endl;//写入文件
- fout.close();
- }
- void main5()
- {
- fstream fio("C:\\3.txt", ios::in | ios::out);
- fio << "锄禾日当午" << endl;//写入文件
- fio << "地雷买下土" << endl;//写入文件
- fio << "谭胜来跳舞" << endl;//写入文件
- fio << "炸成250" << endl;//写入文件
- fio.close();
- {
- fstream fio("C:\\3.txt", ios::in | ios::out);
- for (int i = 0; i < 4; i++)
- {
- char str[50] = { 0 };
- fio.getline(str, 50);
- cout << str << endl;
- }
- fio.close();
- }
- cin.get();
- }
- void main6()
- {
- fstream fio("C:\\4.txt", ios::in | ios::out);
- fio << "锄禾日当午" << endl;//写入文件
- fio << "地雷买下土" << endl;//写入文件
- fio << "谭胜来跳舞" << endl;//写入文件
- fio << "炸成250" << endl;//写入文件
- fio.seekg(ios::beg);//文件指针 ios::beg开始
- for (int i = 0; i < 4; i++)
- {
- char str[50] = { 0 };
- fio.getline(str, 50);
- cout << str << endl;
- }
- fio.close();
- cin.get();
- }
- //写入文件,不需要转换为字符串
- //读取的时候,不需要吧字符串转换为其他类型的操作
- void main7()
- {
- ofstream fout;
- fout.open("C:\\X.txt");
- fout << "ABC" << " " << 123 << " " << 'ch'<<endl;//打印到文件
- fout.close();
- ifstream fin("C:\\X.txt");//创建读取文件的流
- char str[10] = { 0 };//读取字符串
- int num = 0;
- char ch = '\0';
- fin >> str >> num >> ch;
- std::cout << str << "\n" << num << "\n" << ch;
- std::cin.get();
- }
- //读写一个字符
- //文本与二进制存储差别不一样
- void main123213()
- {
- ifstream fin("C:\\4.txt");//创建读取文件的流
- ofstream fout("C:\\40.txt");
- if (!fin || !fout)
- {
- std::cout << "文件打开失败";
- return;
- }
- std::cout << "文件拷贝开始\n";
- char ch=0;
- while (fout && fin.get(ch))//引用的方式读取到一个字符
- {
- fout.put(ch);//写入一个字节
- }
- fin.close();
- fout.close();
- std::cout << "文件拷贝完成";
- cin.get();
- }
- void main10()
- {
- ofstream fout("C:\\40.txt",ios::app);//追加
- fout << "天下英雄,谭胜第一";
- fout.close();
- cin.get();
- }
iosQT
使用TightVNC软件,远程连接mac机器进行开发。
TightVNC
一款用于windows操作系统的应用软件,是一款远程控制软件。
使用教程
1、需要在被控方电脑上打开TvnServer,记住端口的相应信息;
2、如果需要密码验证,在主密码处设置好密码即可,TightVNC的界面也是非常友好的。
字符文件读写二进制与文本差别
文本文件
1 2 3
00000001 00000010 00000011
二进制
123
01111011
get与getline挖掘数据
- #include<iostream>
- #include <stdlib.h>
- using namespace std;
- void main1()
- {
- {
- char buf[80];
- cin.get(buf, 80, '#');//提取一段文本,最大长度为80,遇到#结束
- std::cout << buf;
- }
- system("pause");
- std::cin.get();
- std::cin.get();
- }
- void main2()
- {
- {
- char buf[80];
- cin.get(buf, 80);//以回车结束,最大长度为80
- cin >> buf;//cin无法区分空格
- std::cout << buf;
- }
- system("pause");
- std::cin.get();
- std::cin.get();
- }
- void main3()
- {
- {
- char buf[8];
- cin.get(buf, 8,'n');//如果记录回车,空格,可以以任何字符
- //cin >> buf;//cin无法区分空格
- std::cout << buf;
- }
- system("pause");
- std::cin.get();
- std::cin.get();
- }
- void main4()
- {
- {
- char buf[80];
- cin.get(buf, 40, 'n');//如果记录回车,空格,可以以任何字符
- std::cout << buf<<"\n";//n意味着结束,后面不会读取
- cin.get(buf, 40, 'n');
- std::cout << buf << "\n";
- }
- system("pause");
- std::cin.get();
- std::cin.get();
- }
- void main()
- {
- char buf[80];
- //默认/n,可以设定,可以反复读取
- cin.getline(buf, 80,',');//逐行读取
- std::cout << buf << "\n";
- cin.getline(buf, 80,',');//逐行读取
- std::cout << buf << "\n";
- cin.getline(buf, 80, ',');//逐行读取
- std::cout << buf << "\n";
- cin.getline(buf, 80, '\n');//逐行读取
- std::cout << buf << "\n";
- //cin.get(buf, 80,'x');//一次性读取,以X为结束
- //std::cout << buf << "\n";
- system("pause");
- std::cin.get();
- std::cin.get();
- }
二进制与文本差别
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- struct MyStruct
- {
- char *p = "北京是帝都";
- int num = 20;
- double db = 10.98;
- char ch = 'a';
- };
- void main1()
- {
- ofstream fout("C:\\wen.txt",ios::out);
- ifstream fin("C:\\wen.txt");
- std::cout << sizeof(MyStruct) << std::endl;
- MyStruct my1;
- fout << my1.p <<" "<< my1.num <<" "<< my1.db <<" "<< my1.ch << "\n";
- fout.close();
- char str[100] = { 0 };
- fin.getline(str, 100, 0);//提取
- std::cout << str << std::endl;
- fin.close();
- cin.get();
- }
- void main()
- {
- MyStruct my1;
- my1.p = "chuheridangwu";
- ofstream fout("C:\\bin.bin", ios::binary);
- fout.write((char *)&my1, sizeof(my1));//
- //第一个参数是要写入文件的内存的首地址,
- //第二个参数是长度
- fout.close();
- ifstream fin("C:\\bin.bin", ios::binary);
- MyStruct newmy1;
- fin.read((char*)&newmy1, sizeof(newmy1));
- //保存文件读取到内存,内存首地址
- //长度
- std::cout << newmy1.p << std::endl;
- fin.close();
- std::cin.get();
- }
二进制文件读写
字节的二进制
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- //按照字节的方式读写二进制,
- //文件加密解密需要字节的方式
- void main1()
- {
- ifstream fin("C:\\write.exe", ios::binary);
- ofstream fout("C:\\newwrite.exe", ios::binary);
- if (!fin || !fout)
- {
- std::cout << "文件打开失败";
- return;
- }
- std::cout << "文件拷贝开始\n";
- char ch = 0;
- while (fout && fin.get(ch))//引用的方式读取到一个字符
- {
- fout.put(ch);//写入一个字节
- }
- fin.close();
- fout.close();
- std::cout << "文件拷贝完成";
- std::cin.get();
- }
二进制内存文件的拷贝
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- struct MyStruct
- {
- int i;
- double db;
- };
- void main()
- {
- ofstream fout("C:\\big.txt", ios::binary);
- MyStruct ss[5] = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 }, { 4, 4.4 }, { 5, 5.5 } };
- fout.write((char *)ss, sizeof(MyStruct)* 5);
- fout.close();
- ifstream fin("C:\\big.txt", ios::binary);
- MyStruct *p = new MyStruct[5];//动态分配内存
- fin.read((char *)p, sizeof(MyStruct)* 5);
- fin.close();
- for (int i = 0; i < 5; i++)
- {
- std::cout << p[i].i << " " << p[i].db << std::endl;
- }
- cin.get();
- }
随机位置文本二进制读写
随机文本文本读写
- #include<iostream>
- #include <fstream>
- using namespace std;
- void main1()//随机位置读取
- {
- ofstream fout("p.txt");
- fout << "1234567890abcdefghijklmn";
- fout.close();
- ifstream fin("p.txt");
- //fin.seekg(9, ios::beg);//从开始,往前9个字符
- fin.seekg(-9, ios::end);//从尾部,倒数9个字符
- char ch;
- while (fin.get(ch))
- {
- cout << ch;
- }
- fin.close();
- cin.get();
- }
- void main2()
- {
- ofstream fout("px.txt");
- fout << "1234567890abcdefghijklmn";
- fout.close();
- ofstream Fout("px.txt", ios::in | ios::out);
- char str[] = "ABCDEFG";
- Fout.seekp(0, ios::end);//随机位置进行读写
- long size = Fout.tellp();//当前位置距离begin有多少个字节,尾部可以获取文件大小
- cout << size<<endl;
- Fout << str;
- Fout.close();
- ifstream fin("px.txt");
- char ch;
- while (fin.get(ch))
- {
- cout << ch;
- }
- fin.close();
- cin.get();
- }
随机二进制文本读写
- #include<iostream>
- #include <fstream>
- using namespace std;
- void main1a()
- {
- double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };
- ofstream fout("N.txt", ios::binary);
- fout.write((char *)db, 80);
- fout.close();
- double *p = new double[10];
- ifstream fin("N.txt", ios::binary);
- fin.read((char *)p, 80);
- fin.close();
- for (int i = 0; i < 10; i++)
- {
- std::cout << p[i] << endl;
- }
- std::cin.get();
- }
- void main2b()//随机位置读取
- {
- double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };
- ofstream fout("N.txt", ios::binary);
- fout.write((char *)db, 80);
- fout.close();
- double *p = new double[5];
- ifstream fin("N.txt", ios::binary);
- fin.seekg(-40, ios::end);//读写
- fin.read((char *)p, 40);
- fin.close();
- for (int i = 0; i < 5; i++)
- {
- std::cout << p[i] << endl;
- }
- std::cin.get();
- }
- void main()
- {
- //先读取
- double *p = new double[10];
- ifstream fin("N.txt", ios::binary);
- fin.read((char *)p, 80);
- fin.close();
- for (int i = 0; i < 10; i++)
- {
- std::cout << p[i] << endl;
- }
- //随机写入
- double db[] = { 100.9, 200.8, 300.7, 400.6, 500.5 };
- //ios::in | ios::out不再清零文件,否则会清零
- ofstream fout("N.txt", ios::binary|ios::in | ios::out);
- //fout.seekp(40, ios::beg);
- fout.seekp(0, ios::end);//写入
- fout.write((char *)db, 40);
- fout.close();
- //再次读取
- {
- double *p = new double[20];
- ifstream fin("N.txt", ios::binary);
- fin.read((char *)p, 160);
- fin.close();
- for (int i = 0; i < 20; i++)
- {
- std::cout << p[i] << endl;
- }
- }
- std::cin.get();
- }
多线程初级
- #include <iostream>
- #include <thread>
- #include <string>
- using namespace std;
- void helloworld()
- {
- std::cout << "你好天朝" << endl;
- }
- void helloworldA()
- {
- std::cout << "你好天朝A" << endl;
- }
- void helloworldB()
- {
- std::cout << "你好天朝B" << endl;
- }
- void main1()
- {
- std::thread t1(helloworld);//线程顺序执行
- std::thread t2(helloworldA);
- std::thread t3(helloworldB);
- cin.get();
- }
- #include <iostream>
- #include <thread>
- #include <string>
- #include<windows.h>
- using namespace std;
- void run(int num)
- {
- Sleep(1000);
- std::cout << "你好天朝"<< num<< endl;
- }
- void main2()
- {
- //thread t[10];
- thread *p[10];
- for (int i = 0; i < 10;i++)
- {
- p[i]=new thread(run, i);//循环创建线程
- p[i]->join();//等待
- //p[i]->detach();//脱离当前主线程自由执行,乱序
- }
- cin.get();
- }
线程之间通信以及锁定
- #include <iostream>
- #include <thread>
- #include <string>
- #include<windows.h>
- using namespace std;
- //两个线程并行访问一个变量
- int g_num = 20;//找到或者找不到的标识
- void goA(int num)
- {
- for (int i = 0; i < 15; i++)
- {
- Sleep(1000);
- if (i == 6)
- {
- g_num = 5;
- }
- if (g_num ==5)
- {
- std::cout << "线程" << num << "结束 \n";
- return;
- }
- std::cout << "线程" << num <<" "<< g_num << endl;
- }
- }
- void goB(int num)
- {
- for (int i = 0; i <150; i++)
- {
- Sleep(1000);
- if (g_num == 5)
- {
- std::cout << "线程" << num << "结束 \n";
- return;
- }
- std::cout << "线程" << num << " " << g_num << endl;
- }
- }
- void main3()
- {
- thread t1(goA, 1);
- thread t2(goB, 2);
- t1.join();
- t2.join();
- std::cin.get();
- }
线程锁定
- #include <iostream>
- #include <thread>
- #include <string>
- #include<windows.h>
- #include<mutex>
- using namespace std;
- //两个线程并行访问一个变量
- int g_num = 20;//找到或者找不到的标识
- mutex g_mutex;
- void goA(int num)
- {
- g_mutex.lock();//你访问的变量,在你访问期间,别人访问不了
- for (int i = 0; i < 15; i++)
- {
- Sleep(300);
- g_num = 10;
- std::cout << "线程" << num << " " << g_num << endl;
- }
- g_mutex.unlock();
- }
- void goB(int num)
- {
- for (int i = 0; i < 15; i++)
- {
- Sleep(500);
- g_num = 11;
- std::cout << "线程" << num << " " << g_num << endl;
- }
- }
- void main()
- {
- thread t1(goA, 1);
- thread t2(goB, 2);
- t1.join();
- t2.join();
- std::cin.get();
- }