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

时间:2019-05-18 14:44:21

标签: c++

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

我创建了一个包含10行的文件,并像下面的insertAtLine(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";

}

1 个答案:

答案 0 :(得分:1)

正如其他人所建议的那样(并且根据某些专业人士的说法,最好的方法)是先读取文件的所有行并将其存储到内存中,然后将它们全部一遍一遍地写回去,但要多出一行(在两者之间)当行计数器达到所需的行号时!

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

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

int insertAtLine(const std::string file_name, const std::string line, std::vector::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;
}

首先,当文件作为只读文件打开时,使用类型std::vector<std::string>的向量存储行。第二,当文件作为只写文件打开时,将相同的矢量粘贴回该文件,但这次在所需的行上,多余的行首先粘贴!

至:

for (std::string one_line; std::getline(file, one_line);lines.push_back(one_line));

它等于:

{
    //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);
    }
}

至:

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;
        }

它等于:

{
    //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;
        }
    }
}

用法:

int main(int argc, char* argv[]) {

    std::string file_name;
    std::string line;
    std::string::size_type lineNumber;

    std::cin >> file_name;
    std::cin >> line;
    std::cin >> lineNumber;

    return insertAtLine(file_name, line, lineNumber);
}

文件为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

还要注意如何使用return insertAtLine(file_name, line, lineNumber);向用户报告读/写问题!

祝你好运!