如何从文本文件的末尾删除新行

How do I delete a new line from the end of a text file?

本文关键字:删除 新行 文件 文本      更新时间:2023-10-16

我从文本文件中删除一行,除了用户指定要删除的一行外,我将每一行复制到一个新的文本文件(temp.txt),然后删除原始文本文件(staffMembers.txt)并将temp.txt重命名为staffMembers.txt。

staffMembers.txt的内容如下:

1 | First Person | Manager | 123.45
2 | Second Person | Full Time | 123.45
3 | Third Person | Casual | 123.45

当staffMembers.txt的内容复制到temp.txt时,如下所示:

1 | First Person | Manager | 123.45
2 | Second Person | Full Time | 123.45
3 | Third Person | Casual | 123.45
*** new line ***

有人可以解释我如何可以防止在文本文件的末尾创建的新行?我的代码如下:

void deleteStaffMember()
{
    outStream.open("temp.txt", ios::app);
    if(outStream.fail())
    {
        cout << "Unable to open temp.txt.n";
        exit(1);
    }
    inStream.open("staffMembers.txt");
    if(inStream.fail())
    {
        cout << "Unable to open staffMembers.txt.n";
        exit(1);
    }
    else
    {
        cout << endl << "Please enter a staff members ID to delete: ";
        cin >> deleteLine;
        while(getline(inStream, line))
        {
            string firstCharacter;
            firstCharacter += line[0];
            if (firstCharacter != deleteLine)
            {
                outStream << line << endl;
            }
            else
            {
                inStream.close();
                outStream.close();
                cout << "Error. Unable to find staff member.n" << endl;
                break;
            }
        }
    }
    inStream.close();
    outStream.close();
    remove("staffMembers.txt");
    rename("temp.txt", "staffMembers.txt");
    cout << "The staff member has been deleted successfully.n" << endl;
    system("pause");
    system("cls");
}

无法从outStream中删除endl <<线& lt; & lt;因为temp.txt的格式如下:

1 | First Person | Manager | 123.452 | Second Person | Full Time | 123.453 | Third Person | Casual | 123.45

稍微改变一下逻辑,如下所示:

    bool firstLine = true;
    while(getline(inStream, line))
    {
        string firstCharacter;
        firstCharacter += line[0];
        if (firstCharacter != deleteLine)
        {
            if (!firstLine)
            {
                outStream << endl;
            }
            outStream << line;
            firstLine = false;
        }
        else
        {
            inStream.close();
            outStream.close();
            cout << "Error. Unable to find staff member.n" << endl;
            break;
        }
    }

这样我们在每行之前打印新行,除了第一行。

使用向量

string line;
vector<string> lines;
// put all lines into a vector  
while ( getline(inStream, line) ) {
  lines.push_back( line );
}
// remove the line begin with character deleLine....   
std::remove_if( lines.begin(), lines.end(), 
                [](string &line) { return line[0] == deleteLine; } );
// append newline to every line except last line  
std::for_each( lines.begin(), line.end() - 1,
                []( string &line ) { line += 'n'; } );
// write lines to new file
std::for_each( lines.begin(), line.end(),
                []( string &line ) { outStream << line; } );

代码:

if (firstCharacter != deleteLine)
{
    outStream << line << endl;
}

很可能是罪魁祸首。

它输出一行,然后加上'n',这是'endl'所做的事情之一。

考虑'line'是空字符串或几乎是空字符串的情况。

如果您使用以下代码,行为将变得相当明显:

if (firstCharacter != deleteLine)
{
    outStream << "{" << line << "}" << endl;
}

最后,决定你想如何迎合最后一行。yngum的回答很好,我建议理解他的代码。

(提示:'n'实际上并不表示"行结束",它可以解释为"行分隔符","新行开始"或"行结束"中的任何一个)

添加if语句

if(line != ""){
    Myfile << line << endl ; 
    }