如何从文本文件的末尾删除新行
How do I delete a new line from the end of a text file?
我从文本文件中删除一行,除了用户指定要删除的一行外,我将每一行复制到一个新的文本文件(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的回答很好,我建议理解他的代码。
添加if语句
if(line != ""){
Myfile << line << endl ;
}
相关文章:
- 使用新行和不使用新行读取文件
- 遇到新行时,有没有办法停止istream_iterator
- 如果我真的真的想从 STL 容器继承,并且我继承构造函数并删除新运算符,会发生什么?
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 新行上的 clang 格式右括号
- C++流:保存到新行而不是覆盖旧行
- (C++) 使用诅咒打印从文件中读入的新行
- 按升序输出整数时,将最后一个整数放在新行上
- 用C++计算输入文本的新行数
- 为什么我的代码在终端中没有输出任何内容.开始新行没有错误
- 如何删除新分配的字符,这也是函数返回值?
- 如何在C++中删除新行字符
- 从字符字符串 c++ 中删除新行
- 在临时指针上使用"delete"后程序崩溃,即使只剩下新行和删除行?
- 如何从文本文件的末尾删除新行