文件输入和指针所有权语义



我知道有两种方法可以将文件内容读取到C样式字符串中。我需要C样式字符串而不是unique_ptr<string>的原因是,我将把这些数据传递给C函数gumbo_parse(),它的参数是C样式字符串,这样我就避免了unique_ptr<string>的开销。我对反对我的决定持开放态度。

std::ifstream ifs(bf::path("..."));                                              // Input file stream

第一种方式:

std::filebuf * ptr_fbuff = ifs.rdbuf();                                         // Buffer to file contents.
std::size_t fbuff_size = ptr_fbuff->pubseekoff(0, ifs.end, std::ios_base::in);  // Get offset from 0 to end of buffer.
ptr_fbuff->pubseekpos(0, std::ios_base::in);                                    // Move buffer position back to beginning.
char * buffer = new char[fbuff_size];                                           // Buffer to store file data.
ptr_fbuff->sgetn(buffer, fbuff_size);                                           // Get file data.

第二种方式:

std::stringstream sstm_buffer;                                                  // Buffer to file contents.
sstm_buffer << ifs.rdbuf();                                                     // Read file contents to sstm.
const std::string & str_buffer = sstm_buffer.str();                             // Get underlying string from sstm.
char * buffer = new char[str_buffer.size()];                                    // Buffer to store file data.
str_buffer.copy(buffer, str_buffer.size());                                     // Copy file contents to buffer.

做事:

GumboOutput * ptr_outpt = gumbo_parse(buffer);
//...

关闭文件:

gumbo_destroy_output(&kGumboDefaultOptions, ptr_outpt);
ifs.close();                                                                    // Close stream to file.
delete[] buffer;                                                                // Delete allocated buffer.

两者在内存成本和速度方面有什么区别。显然,在将内容放入C样式字符串之前,一种方法使用Stringstream作为缓冲区,另一种方法在将内容装入C样式字符串前使用filebuf。

现在是我的第二个问题。所有权语义和内存分配。ifstream是否在堆中为rdbuf返回的缓冲区分配任何内存?我是否负责删除rdbuf返回的缓冲区?如果没有,就说我在读一个大文件。。。这不是潜在的大量数据要存储在堆栈中吗?

编辑:

std::unique_ptr<std::string> mytype::my_func(const std::string & path)
{
    std::ifstream ifs(path);                                            // Input file stream
    std::stringstream sstm_buffer;                                      // Buffer to file contents.
    sstm_buffer << ifs.rdbuf();                                         // Read file contents to sstm.
    ifs.close();                                                        // Close stream to file.
    auto ptr_buffer = std::make_unique<std::string>(sstm_buffer.str()); // Pointer to copy of sstm buffer contents.
    return std::move(ptr_buffer);                                       // Return pointer.
}

第2版:

std::string mytype::my_func(const std::string & path) const
{
    std::ifstream ifs(path);
    std::stringstream sstm_buf;
    sstm_buf << ifs.rdbuf();
    ifs.close();
    return sstm_buf.str();
}

第3版:

std::string psclient::file_get(const std::string & path) const
{
    std::ifstream ifs(path);    // Stream to file (Automatically close() on ~).
    std::ostringstream reader;  // Stream to read file contents.
    reader << ifs.rdbuf();      // Read in file contents.
    return reader.str();        // Return a move instead of copy (Done implicitly).
}

我需要C样式字符串而不是unique_ptr的原因是,我将把这些数据传递给C函数

你仍然可以得到一个C风格的指针,例如

#include <iostream>
#include <string>
#include <memory>
void print_C_style_string(const char* psz)
{
    printf("str = %s", psz);
}
int main()
{
    std::unique_ptr<std::string> p{new std::string{"This is a string!"}};
    print_C_style_string(p.get()->c_str());
    return 0;
}

unique_ptr没有开销。摘自Bjarne Stroustrup的"C++编程语言":

与正确使用内置指针相比,unique_ptr是一种非常轻量级的机制,没有空间或时间开销。

将文件逐行读取为字符串的简单方法:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    ifstream fin("my_file.txt", ios::in);
    if(!fin){
        printf("Error opening file.");
        return 1;
    }
    while(fin.peek() != EOF){
        string s;
        getline(fin, s);
        cout << s << std::endl;
    }
    fin.close();
    return 0;
}

在编写现代C++时,有一组特殊的情况需要使用指针,但这不是其中之一。当你不必使用它们时,你应该总是不喜欢使用它们。返回值优化(RVO)和RAII是很棒的东西!

默认情况下,在堆栈上创建的std::string使用堆来存储其内部字符数据;因此,在您的情况下,无需担心堆栈内存有限。此外,std::string::c_str()将返回字符串的内部const char*数据,这正是gumbo_parse()接受的参数。

这简化了读取文件所需的代码。RVO将防止不必要的拷贝,提高性能:

std::string get_file_contents(const std::string& filename)
{
    std::ifstream ifs{filename}; // Open the input file stream
    std::ostringstream ss;       // stringstream to retrieve file data
    ss << ifs.rdbuf();           // Read file data into ss buffer
    return ss.str();             // Return the ss buffer as string (RVO)
}

现在,您不必担心显式释放任何内存。您可以使用上面的函数并将结果字符串的c_str()传递给gumbo_parse():

const std::string file_contents{get_file_contents("input.txt")};
GumboOutput* output = gumbo_parse(file_contents.c_str());
// ...
gumbo_destroy_output(&kGumboDefaultOptions, output);

如果你真的想在某个地方尝试智能指针,你可以尝试用一个自定义的deleter将你的GumboOutput封装在一个指针中;像这样的东西:

using namespace std::placeholders;  // for _1, _2, _3 ...
auto cleanup = std::bind(gumbo_destroy_output, &kGumboDefaultOptions, _1);
std::unique_ptr<int, decltype(cleanup)> output{
    gumbo_parse(file_contents.c_str()), cleanup };

unique_ptr现在将在其托管的GumboOutput上自动调用gumbo_destroy_output,当它被销毁时(即,当它超出范围时)。请自行使用。

最新更新