简体   繁体   English

读取多行文件并使用rdbuf()

[英]Read multiline file and using rdbuf()

I've a multiline file, every line a string. 我有一个多行文件,每行一个字符串。

Example of code.txt : 示例code.txt

AAAAA
BB33A
C544W

I have to put some code contained in another file before every string. 我必须在每个字符串之前放入另一个文件中包含的一些代码。 I'm using this: 我正在使用这个:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    //the file to include before every string
    ifstream one("1.txt");

    //final output file
    ofstream final;
    final.open ("final.txt", ofstream::app);

    //string file
    string line;
    ifstream file("code.txt");

    while (getline(file,line))
    {
       final<<one.rdbuf();
       final<<line;
    }
}

Now, this doesn't work, it works only for the first line of code.txt . 现在,这行不通,仅适用于code.txt的第一行。 What is wrong? 怎么了?

final<<one.rdbuf() works for only the first line because once you stream out the rdbuf the first time, its read pointer is sitting at the end of the 1.txt file data. final<<one.rdbuf()仅适用于第一行,因为一旦您第一次将rdbuf流出,其读指针就位于1.txt文件数据的末尾。 There is no data left for it to read on subsequent streaming. 没有剩余数据供后续流式传输读取。 You would have to reset the one stream back to the beginning of its data on each loop iteration, eg: 您必须在每次循环迭代中将one流重置回其数据的开头,例如:

while (getline(file,line))
{
   final<<one.rdbuf();
   final<<line;
   one.seekp(0); // <-- add this
}

Otherwise, do as @Check suggests. 否则,请按照@Check的建议进行操作。 Read the content of the 1.txt file into memory one time, and then stream that out on each loop iteration, instead of re-reading the file on each iteration. 一次将1.txt文件的内容读入内存,然后在每次循环迭代中将其流式传输出去,而不是在每次迭代时重新读取该文件。

I would change your final<<one.rdbuf(); 我会更改您的final<<one.rdbuf(); . You can use this: 您可以使用此:

//the file to include before every string
ifstream one("1.txt");

std::string first;
if (one.is_open())
{
    // file length and reserve the memory.
    one.seekg(0, std::ios::end);
    first.reserve(static_cast<unsigned int>(one.tellg()));
    one.seekg(0, std::ios::beg);

    first.assign((std::istreambuf_iterator<char>(one)),
                 (std::istreambuf_iterator<char>()));

    one.close();
}

//final output file
ofstream final;
final.open ("final.txt", ofstream::app);

//string file
string line;
ifstream file("code.txt");

while (getline(file,line))
{
    final<<first;
   final<<line;
}

Maybe you want to check this https://stackoverflow.com/a/8737787/3065110 也许您想检查一下这个https://stackoverflow.com/a/8737787/3065110

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“ifstream”,“stringstream”和“rdbuf()”将文件内容读入字符串时如何检查I / O错误? - How to check for I/O errors when using “ifstream”, “stringstream” and “rdbuf()” to read the content of a file to string? 使用rdbuf将文件内容复制到缓冲区失败 - copy file content to a buffer using rdbuf failed 使用rdbuf() - &gt; sputn(...)vs operator &lt; - using rdbuf()->sputn(…) vs operator<< 使用rdbuf复制文件并从文件中读取不一致? - Copying file with rdbuf and inconsitent reading from the file? 使用rdbuf复制流在空输入时失败 - Copy streams using rdbuf fails on empty input 使用str()和rdbuf()打印出字符串流 - Printing out a stringstream using str() and rdbuf() rdbuf()-&gt;pubsetbuf() 使用双向 fstream 仅适用于写入 - rdbuf()->pubsetbuf() using a bidirectional fstream is applied only to writes 使用 rdbuf 重定向 cerr:设置一次并忘记它,直到应用程序结束 - Redirect cerr using rdbuf: set it once and forget it until app ends Boost.IOStreams:如何使用“ rdbuf”正确重定向文件流? - Boost.IOStreams: How to correctly redirect file streams with 'rdbuf'? 在C ++中使用rdbuf()和运算符&lt;复制流时出错 - Error during copying stream in c++ using the rdbuf() and operator <<
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM