将文件(.txt,.pdf ..)转换为流文件 [英] convert a file (.txt,.pdf..) into a stream file

查看:591
本文介绍了将文件(.txt,.pdf ..)转换为流文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我做了以下操作:

  //转换为流:
std :: string fil =/home/file.pdf;

std :: ifstream t(fil.c_str());
if(t)
{
string res;
string line;
while(getline(t,line,'\\\
'))
{
res = res + line;
}
std :: string p;
p =(base64_encode((reinterpret_cast< const unsigned char *>(res.c_str())),res.size()));
std :: string data = p;

char * token = strtok(const_cast< char *>(fil.c_str()),/);
std :: string name;
std :: vector< int>值;

while(token!= NULL)
{
name =
token = strtok(NULL,/);
}
std :: string f_name = name;
}

//从流转换为文件
ofstream myfile;

std :: string fil;

ofstream文件(fil.c_str(),ios :: out | ios :: binary);
std :: string content = base64_decode(f_data);
file.write((char *)& content,sizeof(content));
file.close();

这是最简单的方法吗?是否可以升级我的代码?



EDIT



代码适用于.cpp或.txt文件。它不适用于.pdf文件。为什么?

解决方案

据我所知,最简单的方法读取文件到一个字符串C ++ 03标准库设施是:

  #include< iostream> 
#include< fstream>
#include< sstream>
#include< string>

std :: string readfile(const std :: string& path)
{
std :: ostringstream contents;
std :: ifstream file(path.c_str(),std :: ios :: binary);
contents<< file.rdbuf();
return(contents.str());
}

然后,您可以继续进行要应用的任何处理:

  std :: cout< readfile(foo.txt)<< std :: endl; 

如果要应用base 64编码,我将从您的代码中假设以下签名,一个方便的重载:

  std :: string base64_encode(const unsigned char * data,std :: size_t size); 

std :: string base64_encode(const std :: string& contents)
{
const unsigned char * data =
reinterpret_cast< const unsigned char *> contents.data());
return(base64_encode(data,contents.size()));
}

您可以这样调用:

  //读取foo.txt文件内容,然后base 64编码二进制数据。 
const std :: string data = base64_encode(readfile(foo.txt));


I would like to ask what is the easiest and faster way to convert a file into a stream file.

I did the following :

//convert to stream:
std::string fil= "/home/file.pdf";

std::ifstream t(fil.c_str());
if (t)
{
  string res;
  string line;
  while (getline(t, line, '\n'))
  {
    res=res+line;
  }
  std::string p;
  p=(base64_encode((reinterpret_cast<const unsigned char *> (res.c_str())),res.size()));
  std::string data=p;

  char *token = strtok( const_cast<char*>(fil.c_str() ), "/" );
  std::string name;
  std::vector<int> values;

  while ( token != NULL )
  {
    name=token;
    token = strtok( NULL, "/" );
  }
  std::string f_name=name;
}

//convert from stream to file
ofstream myfile;

std::string fil;

ofstream file (fil.c_str(), ios::out | ios::binary);
std::string content = base64_decode(f_data);
file.write ((char*)&content, sizeof(content));
file.close();

Is this the easiest way?! Is there a posibility to upgrade my code?

EDIT

Code works for .cpp or .txt files. It doesn't work for .pdf files. why?

解决方案

As far as I know, the easiest way to read a file into a string (byte for byte) using only C++03 standard library facilities is:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

std::string readfile ( const std::string& path )
{
    std::ostringstream contents;
    std::ifstream file(path.c_str(), std::ios::binary);
    contents << file.rdbuf();
    return (contents.str());
}

Then, you can proceed to whatever processing you want to apply:

std::cout << readfile("foo.txt") << std::endl;

If you want to apply base 64 encoding, I'll assume the following signature from your code, and a convenient overload:

std::string base64_encode( const unsigned char * data, std::size_t size );

std::string base64_encode ( const std::string& contents )
{
    const unsigned char * data =
        reinterpret_cast<const unsigned char*>(contents.data());
    return (base64_encode(data, contents.size()));
}

Which you can invoke as such:

// Read "foo.txt" file contents, then base 64 encode the binary data.
const std::string data = base64_encode(readfile("foo.txt"));

这篇关于将文件(.txt,.pdf ..)转换为流文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆