[C/C++标准库]_[中级]_[使用stringstream作为内存流读取]


场景:

1. C++标准string是可以支持unsigned char* 字节数据的,只要使用以下两个函数就可以添加即使是0x0的数据.

string& append( const char* str, size_type num );
string( const char* str, size_type length );

2.stringstream可作为内存流来使用,比如再需要处理文件,过滤内容后再输出到另一个文件里。内存流不需要操作文件指针,比较省时间和io.

流向: ifstream(FILE*)->ostringstream->istringstream->ofstream(FILE*)


http://www.cplusplus.com/reference/ios/

[C/C++标准库]_[中级]_[使用stringstream作为内存流读取]_第1张图片


测试代码: test_stream.cpp

#include <locale>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <string.h>
#include <sstream>
using namespace std;

void TestMemoryStream()
{
	unsigned char buf[] = {0xf1,0x0,0xfe,0x00};
	string str((char*)buf,4);
	size_t str_size = str.size();
	cout << "str_size: " << str_size << endl;
	cout << (int)str[1] << endl;

	istringstream iss(str,ios_base::in);
	iss.seekg(0,ios_base::beg);
	char buffer[4];
	memset(buffer,0,sizeof(buffer));
	iss.read(buffer,1);
	cout << (int)(buffer[0] & 0xff) << endl;

	iss.seekg(0,ios_base::end);
	size_t last_pos = iss.tellg();

	cout << "stream size: " << last_pos  << endl;

}

//http://www.cplusplus.com/reference/ios/
int main(int argc, char const *argv[])
{
	//1.测试内存流.
	TestMemoryStream();

	//2.文件流->内存流->文件流.
	//ifstream->ostringstream->istringstream->ofstream
	cout << argv[0] << endl;
	ifstream is(argv[0],ios_base::binary);
	ostringstream oss(ios_base::binary);

	streamsize ss_size = 0;
	char buf[65];
	cout << "begin read is" << endl;
	while(!is.eof())
	{
		is.read(buf,64);
		ss_size = is.gcount();
		if(ss_size)
		{
			oss.write(buf,ss_size);
		}
	}

	cout << "end read is" << endl;
	istringstream iss(oss.str(),ios_base::binary);
	iss.seekg(0,ios_base::beg);

	string str_path(argv[0]);
	str_path.append(".bak");
	ofstream ioo(str_path.c_str(),ios_base::binary);
	
	cout << "start read iss" << endl;
	while(!iss.eof())
	{
		iss.read(buf,64);
		ss_size = iss.gcount();
		if(ss_size)
		{
			ioo.write(buf,ss_size);
		}
	}
	cout << "end read iss" << endl;

	return 0;
}

输出:

C:\Users\apple\Desktop>test_ifstream.exe
str_size: 4
0
241
stream size: 4
test_ifstream.exe
begin read is
end read is
start read iss
end read iss



你可能感兴趣的:(C++,内存流,stringstream)