是否有一种C/C++方法使用read()从套接字读取数据并使接收缓冲区是文件(ofstream)或类似的自扩展对象(例如)?
编辑: 当我考虑如何读取可能接收10000+字节文件的内容的流套接字时出现了问题.我从来没有像把20000或50000字节(现在足够大)放在堆栈上作为缓冲区,文件可以临时存储,直到我可以插入文件.为什么不直接将其直接流入文件中以加注星标.
就像你可以在std:string里面的char*那样,我想到了类似的东西
read( int fd, outFile.front(), std::npos ); // npos = INT_MAX
Run Code Online (Sandbox Code Playgroud)
或类似的东西.
结束编辑
谢谢.
这很简单,而且是我无法理解的,但我认为沿着这些思路可以解决问题:
template <unsigned BUF_SIZE>
struct Buffer {
char buf_[BUF_SIZE];
int len_;
Buffer () : buf_(), len_(0) {}
int read (int fd) {
int r = read(fd, buf_ + len_, BUF_SIZE - len_);
if (r > 0) len_ += r;
return r;
}
int capacity () const { return BUF_SIZE - len_; }
}
template <unsigned BUF_SIZE>
struct BufferStream {
typedef std::unique_ptr< Buffer<BUF_SIZE> > BufferPtr;
std::vector<BufferPtr> stream_;
BufferStream () : stream_(1, BufferPtr(new Buffer<BUF_SIZE>)) {}
int read (int fd) {
if ((*stream_.rbegin())->capacity() == 0)
stream_.push_back(BufferPtr(new Buffer<BUF_SIZE>));
return (*stream_.rbegin())->read(fd);
}
};
Run Code Online (Sandbox Code Playgroud)
在评论中,您提到您想避免创建一个大的字符缓冲区。使用read系统调用时,执行几次大的读取通常比执行许多小的读取更有效。因此,大多数实现都会选择大输入缓冲区来获得这种效率。你可以实现类似的东西:
std::vector<char> input;
char in;
int r;
while ((r = read(fd, &in, 1)) == 1) input.push_back(in);
Run Code Online (Sandbox Code Playgroud)
但这将涉及系统调用,并且为每个输入字节至少复制一个字节。相反,我提出的代码避免了额外的数据副本。
我真的不希望我给出的代码成为您会采用的解决方案。我只是想向您展示如何创建一个具有相当空间和时间效率的自扩展对象。根据您的目的,您可能想要扩展它,或者编写您自己的。在我的脑海中,一些改进可能是:
std::list代替,以避免矢量调整大小readv始终允许一次至少读取BUF_SIZE字节(或多于字节)BUF_SIZE