C++ 在文本文件中的特定行插入行

C++ 在文本文件中的特定行插入行,c++,C++,我正在尝试在特定行插入一行 我创建了一个包含10行的文件,并调用了下面的函数,如插入行(3,“somedata”),然而,控制台被卡住了,什么都没有,文本“somedata”也没有保存到文件中 void insertAtLine(int lineNumber,string line) { ofstream myfile("C:\\Users\\test\\Music\\myfile.txt"); int counter=0; if(myfile.is_open())

我正在尝试在特定行插入一行

我创建了一个包含10行的文件,并调用了下面的函数,如插入行(3,“somedata”),然而,控制台被卡住了,什么都没有,文本“somedata”也没有保存到文件中

void insertAtLine(int lineNumber,string line)
{
    ofstream myfile("C:\\Users\\test\\Music\\myfile.txt");

    int counter=0;
    if(myfile.is_open())
    {
        string str;
        do{
            getline(cin, str);
            if(counter == lineNumber)
            {
                myfile<<line<< endl;
            }
            counter++;

        }while(str!="");
        myfile.close();
    }
    else cerr<<"Unable to open file";

}
void插入线(整数行号,字符串行)
{
流myfile(“C:\\Users\\test\\Music\\myfile.txt”);
int计数器=0;
如果(myfile.is_open())
{
字符串str;
做{
getline(cin,str);
如果(计数器==行号)
{
myfile正如其他人所建议的(根据一些专业人士的说法,最好的)方法是首先读取文件中的所有行并将其存储到内存中,然后逐个将它们写回,但当行计数器达到所需的行号时,将多余的行放在中间

一种可能的解决方案如下所示:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int insertAtLine(const std::string file_name, const std::string line, std::vector<std::string>::size_type lineNumber){
    //the memory storage medium
    std::vector<std::string> lines;
    //Reading the file to the storage
    {
        //opening the file for reading from it
        std::ifstream file(file_name);
        //checking if the file has been opened correctly
        if (not file.is_open()) {
            std::cerr << "can't open the file " << file_name << std::endl;
            return -1;
        }
        //Reading
        for (std::string one_line; std::getline(file, one_line);lines.push_back(one_line));
    }
    //Writing the storage to the file
    {
        //opening the file for writing to it
        std::ofstream file(file_name);
        //checking if the file has been opened correctly
        if (not file.is_open()) {
            std::cerr << "can't open the file " << file_name << std::endl;
            return -1;
        }
        //finding out the number of the lines
        const auto lines_count = lines.size();
        //writing
        for (std::string::size_type lines_counter(0); lines_counter < lines_count;  file << lines[lines_counter++] << std::endl){
            //checking the line number and writing the extra line if it is needed
            if(lines_counter == lineNumber) file << line<< std::endl;
        }
    }
    //returning 0 if there was no error to this stage 
    return 0;
}
它等于:

{
    //creating a temporary string to be filled with a line
    std::string one_line;
    //reading one line from the file and putting it in "one_line"" while file has not been ended.
    while( std::getline(file, one_line) ){
        //putting the temporary read line "one_line" in to the vector
        lines.push_back(one_line);
    }
}
{
    //creating a counter for the loop
    std::string::size_type lines_counter = 0;
    //writing a line to the file for the amount of the files original line number.
    while( lines_counter < lines_count ){
        //writing an original line from the vector to the file
        file << lines[lines_counter] << std::endl;
        //incrementing the counter
        ++lines_counter;
        //if the line number is reached writing the extra line to the file
        if(lines_counter == lineNumber){
            file << line<< std::endl;
        }
    }
}
至于:

for (std::string one_line; std::getline(file, one_line);lines.push_back(one_line));
for (std::string::size_type lines_counter(0); lines_counter < lines_count;  file << lines[lines_counter++] << std::endl){
            //checking the line number and writing the extra line if it is needed
            if(lines_counter == lineNumber) file << line<< std::endl;
        }
文件为
input.txt

line 0
line 1
line 2
line 3
line 4
运行时控制台通信:

input.txt
hello
2
然后文件变成:

line 0
line 1
hello
line 2
line 3
line 4
还要注意如何使用
returninsertline(文件名、行、行号);
向用户报告读/写问题


祝你好运!

我看到的最简单的方法是将整个文件作为行读取,并存储为
stf::vector
,插入所需的测试行,然后将向量写回文件。@SamVarshavchik getline是从控制台读取的,而不是从文件读取的!@πάνταῥεῖ 我的意思是,在本例中,它是从cin读取的。@AKL没有一种方法可以提高内存/进程效率吗?是的,但收益通常是最小的,难度要大几个数量级。@AKL您在上周赢得了200个声誉,这实际上相当不错,特别是自从您在不到一个月前开始工作以来。很好答案并不总是被高估,因为它取决于很多其他因素,比如问题的受欢迎程度。继续回答问题,你会做得更好。关于这个网站对初学者很难,这是一个有争议的问题,这里也有。
line 0
line 1
hello
line 2
line 3
line 4