C++ 使用C++;

C++ 使用C++;,c++,C++,所以基本上我在一个txt文件中有这个内容: 506;25; ,事情1事情2事情3;10、15 600;12345678910; whatever1,whatever2,whatever3;15、60 我想将12345678910更改为较短的字符串,例如2。 所以我运行代码,它确实改变了前两个;…;之间的字符串在第2行 运行txt后,如下所示: 506;25; ,事情1事情2事情3;10、15 600;2.whatever1,whatever2,whatever3;15、60 ;15、60 但是,

所以基本上我在一个txt文件中有这个内容:

506;25; ,事情1事情2事情3;10、15

600;12345678910; whatever1,whatever2,whatever3;15、60

我想将
12345678910
更改为较短的字符串,例如2。
所以我运行代码,它确实改变了前两个
;…;之间的字符串在第2行

运行txt后,如下所示:

506;25; ,事情1事情2事情3;10、15

600;2.whatever1,whatever2,whatever3;15、60

;15、60

但是,如果我更改为较大的字符串,它不会创建第三行

while (j < storage_line.size())
{

    size_t found = storage_line[j].find(string_to_add);
    if (found != std::string::npos)
        {
            size_t found2 = storage_line[j].find(";"); //finds the position of the first ;
            size_t found3 = storage_line[j].find(";", found2 + 1); //finds position of the second ;
            storage_line[j].replace(found2 + 2, found3 - found2 - 2, string_to_add);
            file2 << storage_line[j] << endl;
            cout << storage_line[j] << endl;
            j++;
        }
        else
        {
            file2 << storage_line[j] << endl;
            cout << storage_line[j] << endl;
            j++;
        }
}
while(jFILE2你只是把字符串插入中间行,使它更长。你没有写任何新行字符。为什么你会期待一个新的行?@ KnyoStrm?我用的是存储:行[j]。(我想插入新字符串的位置,从我想插入新字符串的位置开始,原始字符串中要删除的字符数,我想添加的字符串)@KennyOstrom with cout可以很好地工作,但在我写入文件时不行否…行可以任意长。它以一个或多个“行尾”字符结尾。请尝试将std::endl写入希望行结束的文件。现在,您只需写入一个较长的字符串,然后是它已经具有的相同的endl,因此行数不会改变。仅仅因为长字符串被包装到屏幕上的第二行中并不意味着什么——这只是在控制台中显示它的一种效果。@KennyOstr我已经弄明白了。我必须归档。打开(“info_textfile.txt”,ios::in)将信息提取到字符串向量close.file()文件。打开(“info_textfile.txt”,ios::trunc | ios::out)然后写我想要的。我必须在写文件之前清理文件。