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


目录
相关文章
|
10月前
|
C++ Windows
.NET Framework安装不成功,下载`NET Framework 3.5`文件,Microsoft Visual C++
.NET Framework常见问题及解决方案汇总,涵盖缺失组件、安装失败、错误代码等,提供多种修复方法,包括全能王DLL修复工具、微软官方运行库及命令行安装等,适用于Windows系统,解决应用程序无法运行问题。
1491 3
|
存储 算法 安全
基于哈希表的文件共享平台 C++ 算法实现与分析
在数字化时代,文件共享平台不可或缺。本文探讨哈希表在文件共享中的应用,包括原理、优势及C++实现。哈希表通过键值对快速访问文件元数据(如文件名、大小、位置等),查找时间复杂度为O(1),显著提升查找速度和用户体验。代码示例展示了文件上传和搜索功能,实际应用中需解决哈希冲突、动态扩容和线程安全等问题,以优化性能。
|
存储 消息中间件 资源调度
C++ 多线程之初识多线程
这篇文章介绍了C++多线程的基本概念,包括进程和线程的定义、并发的实现方式,以及如何在C++中创建和管理线程,包括使用`std::thread`库、线程的join和detach方法,并通过示例代码展示了如何创建和使用多线程。
306 1
C++ 多线程之初识多线程
|
消息中间件 存储 安全
|
存储 并行计算 安全
C++多线程应用
【10月更文挑战第29天】C++ 中的多线程应用广泛,常见场景包括并行计算、网络编程中的并发服务器和图形用户界面(GUI)应用。通过多线程可以显著提升计算速度和响应能力。示例代码展示了如何使用 `pthread` 库创建和管理线程。注意事项包括数据同步与互斥、线程间通信和线程安全的类设计,以确保程序的正确性和稳定性。
562 5
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
825 6
|
C++
C++ 多线程之线程管理函数
这篇文章介绍了C++中多线程编程的几个关键函数,包括获取线程ID的`get_id()`,延时函数`sleep_for()`,线程让步函数`yield()`,以及阻塞线程直到指定时间的`sleep_until()`。
411 0
C++ 多线程之线程管理函数
|
Linux C++
Linux c/c++文件虚拟内存映射
这篇文章介绍了在Linux环境下,如何使用虚拟内存映射技术来提高文件读写的速度,并通过C/C++代码示例展示了文件映射的整个流程。
540 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
504 12

热门文章

最新文章