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

简介: <p></p> <h2><span style="font-family:宋体; font-size:16pt">文件重定向</span><span style="font-family:宋体; font-size:16pt"></span></h2> <img src="http://img.blog.csdn.net/20151103141629604" alt=""><br>

文件重定向



#include
 
  
using namespace std;

void main()
{
	char str[30] = { 0 };
	cin >> str;
	cout << str;
	system(str);

	cerr << "error for you";

	cin.get();
	cin.get();
}
 
 

键盘输入流

#include
 
  
#include 
  
  
   

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
 
  
#include
  
  
   //控制输出流

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"<
   
   
    <
    
     > 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<
    
     

     
      

字符串输入输出

#include
       
        
#include
       
        
         
#include
        
         
          

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 <
         
          
           << endl;
	cout <
          
           
            << 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 <
           
            
              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< 
              
              

字符串流

#define _CRT_SECURE_NO_WARNINGS
#include 
               
                
#include 
               
                
                 
#include 
                
                 
                  
#include 
                 
                  
                   

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"<
                  
                   
                    << endl;


	system("pause");
}
                  
                   
                 
                  
                
                 
               
                
              
               

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

#include 
               
                
#include
               
                
                 

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 << "锄禾日当午"<
                
                 
                  < 4; i++) char str[50]="{" 0 }; fio.getline(str, 50); cout str cin.get(); main6() fio(c:\\4.txt, fio.seekg(ios::beg); 文件指针 ios::beg开始 写入文件,不需要转换为字符串 读取的时候,不需要吧字符串转换为其他类型的操作 main7() ofstream fout; fout.open(c:\\x.txt); abc 123 ch<
                  
                   > 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

getgetline挖掘数据

#include
               
                
#include 
               
                
                 
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
               
                
#include
               
                
                 
#include
                
                 
                  
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
               
                
#include
               
                
                 
#include
                
                 
                  
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
               
                
#include
               
                
                 
#include
                
                 
                  

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
               
                
#include 
               
                
                 
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<
                
                 

                 
                  

随机二进制文本读写

#include
                   
                    
#include 
                   
                    
                     
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 
                   
                    
#include 
                   
                    
                     
#include 
                    
                     
                      

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 
                   
                    
#include 
                   
                    
                     
#include 
                    
                     
                      
#include
                     
                      
                       

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 
                   
                    
#include 
                   
                    
                     
#include 
                    
                     
                      
#include
                     
                      
                       


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 
                   
                    
#include 
                   
                    
                     
#include 
                    
                     
                      
#include
                     
                      
                       
#include
                      
                       
                        

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();
}
                      
                       
                     
                      
                    
                     
                   
                    
                  
                   


目录
相关文章
|
7月前
|
存储 监控 算法
基于 C++ 哈希表算法实现局域网监控电脑屏幕的数据加速机制研究
企业网络安全与办公管理需求日益复杂的学术语境下,局域网监控电脑屏幕作为保障信息安全、规范员工操作的重要手段,已然成为网络安全领域的关键研究对象。其作用类似网络空间中的 “电子眼”,实时捕获每台电脑屏幕上的操作动态。然而,面对海量监控数据,实现高效数据存储与快速检索,已成为提升监控系统性能的核心挑战。本文聚焦于 C++ 语言中的哈希表算法,深入探究其如何成为局域网监控电脑屏幕数据处理的 “加速引擎”,并通过详尽的代码示例,展现其强大功能与应用价值。
176 2
|
7月前
|
监控 算法 数据处理
基于 C++ 的 KD 树算法在监控局域网屏幕中的理论剖析与工程实践研究
本文探讨了KD树在局域网屏幕监控中的应用,通过C++实现其构建与查询功能,显著提升多维数据处理效率。KD树作为一种二叉空间划分结构,适用于屏幕图像特征匹配、异常画面检测及数据压缩传输优化等场景。相比传统方法,基于KD树的方案检索效率提升2-3个数量级,但高维数据退化和动态更新等问题仍需进一步研究。未来可通过融合其他数据结构、引入深度学习及开发增量式更新算法等方式优化性能。
199 17
|
C++ 容器
C++中向量的操作vector
C++中向量的操作vector
195 6
|
数据处理 C++
C++程序字符串流
C++程序字符串流
180 2
|
编译器 开发工具 C++
【Python】已解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build
【Python】已解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build
7450 0
|
算法 前端开发 Linux
【常用技巧】C++ STL容器操作:6种常用场景算法
STL在Linux C++中使用的非常普遍,掌握并合适的使用各种容器至关重要!
327 10
|
C++ iOS开发 开发者
C++一分钟之-文件输入输出(I/O)操作
【6月更文挑战第24天】C++的文件I/O涉及`ifstream`, `ofstream`和`fstream`类,用于读写操作。常见问题包括未检查文件打开状态、忘记关闭文件、写入模式覆盖文件及字符编码不匹配。避免这些问题的方法有:检查`is_open()`、显式关闭文件或使用RAII、选择适当打开模式(如追加`ios::app`)以及处理字符编码。示例代码展示了读文件和追加写入文件的实践。理解这些要点能帮助编写更健壮的代码。
220 2
|
存储 C++
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
247 0
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
208 0
C++职工管理系统(类继承、文件、指针操作、中文乱码解决)
在C和C++中,指针的算术操作
在C和C++中,指针的算术操作