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

sshcrbum  于 2023-01-06  发布在  其他
关注(0)|答案(4)|浏览(227)

我正在从文本文件中删除一行,方法是将除用户指定要删除的一行之外的每一行复制到一个新的文本文件(temp.txt),然后删除原始文本文件(staffMembers.txt)并将temp.txt重命名为staffMembers.txt。
staffMembers.txt的内容如下:
将staffMembers.txt的内容复制到temp.txt时,如下所示:
有人能解释一下我如何防止在文本文件末尾创建新行吗?我的代码如下:

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格式如下:

fdbelqdn

fdbelqdn1#

使用载体的清洁剂溶液

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

plicqrtu2#

只要稍微改变一下逻辑,就像这样:

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

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

bksxznpy

bksxznpy3#

代码:

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

很可能就是罪魁祸首。
它输出行,然后添加"\n",这是"endl"所做的事情之一。
考虑'line'是空字符串或几乎是空字符串的情况。
如果您使用以下代码,这种行为将变得相当明显:

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

最后,决定如何迎合最后一行。yngum的回答非常好,我建议理解他的代码。
(Hint:"\n"实际上并不表示"行尾",它可以解释为"行分隔符"、"新行开始"或"行尾"中的任何一个)

ar7v8xwq

ar7v8xwq4#

只需添加这是if语句

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

相关问题