c ++ /以特定方式在txt文件上附加数据

时间:2015-05-07 03:59:23

标签: c++ text ofstream

我有一个这样的txt文件:

  

123456

     

123456

     

abcdef

     

...

使用C ++,我想打开它并以下列方式添加数据

  

123456新数据新数据

     

123456新数据新数据

     

abcdef新数据新数据

     

...

我已经在stackOverflow上看到了这个post,但它在Python中。

我使用了这个功能

std::ofstream file_to_save;
file_to_save.open(path, ios::out | ios::app);

但它会在文件末尾添加数据,而不是在每个单词的旁边添加数据。

编辑:实际上我在循环中连续添加日期,而不是一次性添加所有内容。这就是困难。所以我的文件包含所有日期然后在循环中,我创建新数据。让我们说“新数据”,然后我想将这些新数据(总是不同的)分配给已经存在的文件。

  

123456

     

123456

     

abcdef

     

...

我在循环“新数据1”中创建新数据,然后想要添加到该文件

  

123456新数据1

     

123456

     

abcdef

     

...

然后在我的循环上的第2步,我创建“新数据2”然后想要添加到该文件

  

123456新数据1

     

123456新数据2

     

abcdef

     

...

然后在我的循环上的第3步,我创建“新数据3”然后想要添加到该文件

  

123456新数据1

     

123456新数据2

     

abcdef新数据3

     

...   依此类推,直到填满整个文件。

有人能帮助我吗?

感谢

5 个答案:

答案 0 :(得分:1)

读取所有文件并将字符串存储在矢量中。对于向量中的每个元素,请附加新数据,然后将其写入文件,完全覆盖文件。

或者,您可以使用搜索功能逐行读取/覆盖您的文件

答案 1 :(得分:1)

您必须将现有文件的内容存储在数组中 或矢量并将其写回同一文件。

void appendToFile(string sFilename, int nInsertAt, string sDataToInsert)
{
    std::ifstream infile("output.txt");

    string line;
    vector<string> vLines;

    while (std::getline(infile, line))
    {
        vLines.push_back(line);
    }

    infile.close();
    std::ofstream outfile("output.txt");

    for (int i = 0; i < vLines.size(); i++)
    {
        char buff[1024];
        if (i == nInsertAt)
        {
            sprintf(buff, "%s %s", vLines[i].c_str(), sDataToInsert.c_str());
            outfile << buff << endl;
        }
        else
        {
            outfile << vLines[i] << endl;
        }

    }

    outfile.close();
}

void test()
{
    for (int i = 0; i < 10; i++)
    {
        char buff[1024];
        sprintf(buff, "new data %d", i);
        appendToFile("output.txt", i, buff);
    }
}

答案 2 :(得分:1)

有很多方法可以做到这一点。最安全的通常是将每一行复制到一个新文件中,将所需的任何内容附加到该行,并继续直到输入文件的末尾。

然后您可以将数据从新文件复制回旧文件,或者(如果您确定没有其他链接)删除旧文件,并将新文件重命名为旧名称。< / p>

或者,您可以从旧文件复制到临时文件,然后在将数据复制回旧文件时处理数据(最后删除临时文件)。

将数据读入内存,然后用新数据覆盖文件要脆弱得多 - 如果在操作过程中遇到崩溃或断电,您的文件可能会被破坏(即,您不要没有旧的新数据的副本。我会避免它,除非你真的更担心速度而不是可靠性,你不介意破坏它的可能性数据完全。

明显的代码:

std::ifstream input("filename");
std::ofstream output("filename2");

std::string line;

while (std::getline(input, line))
    output << line << " new data new data new data\n";

答案 3 :(得分:1)

从我在你的问题中读到的问题,你想要:

  1. 读取行
  2. 向其添加新数据
  3. 存储所有更新的行
  4. 使用新行覆盖文件
  5. 以下是一种可能的实施方式:

    ifstream infile("filetobeupdated.txt");
    if(!infile) error("Can't open file: ", filetobeupdated);
    
    // vector holding old data
    string line;
    vector<string>oldlines;
    
    while(getline(infile,line)) oldlines.push_back(line);
    
    infile.close();
    
    // vector holding newdata
    vector<string>newdata;
    
    // vector holding updated data
    vector<string>updateddata;
    
    // concatenate old line + new data 
    for(size_t i=0, i<oldlines.size();i++) updateddata.push_back(oldlines[i]+newdata[i]);                                                     
    
    // overwrite old file with new data
    ofstream onfile("filetobeupdated.txt");
    if(!onfile) error("Can't open file: ", filetobeupdated);
    
    for(size_t i=0, i<newdata.size();i++) onfile << newdata[i] <<'\n';
    
    onfile.close();
    

答案 4 :(得分:0)

这就是“ios :: app”的工作原理 - 在文件末尾添加数据。

您可能希望先读取每一行,然后覆盖所有行。

相关问题