0

我通过将每一行(用户指定的要删除的一行除外)复制到一个新的文本文件(temp.txt),然后删除原始文本文件(staffMembers.txt)来从文本文件中删除一行。 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 << line << endl;中删除endl ;因为 temp.txt 的格式如下:

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

3 回答 3

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; } );
于 2013-05-02T04:25:47.577 回答
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;
        }
    }

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

于 2013-05-02T03:47:48.530 回答
1

编码:

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

很可能是罪魁祸首。

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

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

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

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

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

(提示:“\n”实际上并不意味着“行尾”,它可以解释为“行分隔符”、“新行开始”或“行尾”中的任何一个)

于 2013-05-02T04:41:22.460 回答