提高:: :: iostream的副本(),InputStream和outstream输出explanantion [英] boost::iostream::copy(), inputstream and outstream output explanantion
问题描述
我有一个txt文件:
GCC-4.7.2.txt:与数据写入:
你好
这是一个测试文件。
谢谢
:COM pressed作为GCC-4.7.2.tar.bz2
I have a txt file: gcc-4.7.2.txt : with the data written: Hello This is a test file. Thanks :compressed as gcc-4.7.2.tar.bz2
现在,我运行下面的code:
Now, I run the following code:
#include <sstream>
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/filesystem.hpp>
int main()
{
using namespace std;
using namespace boost::iostreams;
char filename[] = "gcc-4.7.2.tar.bz2";
if (!boost::filesystem::exists(filename))
{
cout << "Can't find " << filename << ". Expect errors to follow! " << endl;
}
ifstream file(filename, ios_base::in | ios_base::binary);
filtering_streambuf<input> in;
filtering_streambuf<output> out;
in.push(bzip2_decompressor());
in.push(file);
try
{
//cout << "in_file:" << in << endl;
boost::iostreams::copy(in, cout);
//boost::iostreams::copy(in, out);
//cout << cout << endl;
//boost::iostreams::copy(in, compressed_string);
//cout << "Copied" << compressed_string << " " << in.str() << endl;
}
catch (const bzip2_error& exception)
{
cout << "catchblock" << endl;
cout << exception.what() << endl;
int error = exception.error();
if (error == bzip2::data_error)
{
cout << "compressed data stream is corrupted";
}
else if (error == bzip2::data_error_magic)
{
cout << "compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'";
}
else if (error == bzip2::config_error)
{
cout << "libbzip2 has been improperly configured for the current platform";
}
else
{
cout << "Error: " << error;
}
cout << endl;
}
}
在运行它的输出是:
While running it the output is:
dev4@sun-desktop:~/readerwriter$ ./test1
gcc-4.7.2.txt0000644000175100001440000000004312547435102011603 0ustar dev4usersHello
This is a test file.
感谢
什么是你好前的人物?
为什么它打印的文件名?
What are the characters before Hello ? why is it printing the file name?
我刚刚复制的在中流来清点使用boost :: iostream的功能。
I just copied the the in stream 'in' to cout using boost::iostream function.
为什么只被复制到COUT我怎么能有不同的命名输出流呢?
Why should it only be copied to 'cout' how can I have a different named output stream there ?
推荐答案
如果您uncom preSS的COM pressed tar归档,你会得到一个tar归档!
If you uncompress the compressed tar archive, you get a tar archive!
焦油归档格式描述如下: https://en.wikipedia.org/维基/ Tar_(计算)#File_format
Tar archive format is described here: https://en.wikipedia.org/wiki/Tar_(computing)#File_format
要表明是如何工作的,试试这个来代替:
To show how that works, try this instead:
dev4@sun-desktop:~/readerwriter$ ./test1 | tar x
要提取的文件
这篇关于提高:: :: iostream的副本(),InputStream和outstream输出explanantion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!