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

时间:2013-05-02 03:43:32

标签: c++

我正在从文本文件中删除一行,方法是将除了要删除的用户指定的一行除外的每一行复制到新的文本文件(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&lt;&lt; 删除 endl line&lt;&lt; endl; 因为temp.txt的格式如下:

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

3 个答案:

答案 0 :(得分:2)

使用矢量

的清洁解决方案
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; } );

答案 1 :(得分:1)

稍微改变逻辑,如下:

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

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

答案 2 :(得分:1)

代码:

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

可能是罪魁祸首。

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

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

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

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

最后,决定如何迎合最后一行。 yngum的答案非常好,我建议理解他的代码。

(提示:'\ n'实际上并不意味着“行尾”,它可以解释为“行分隔符”,“新行开头”或“行尾”)