删除文件中的空行
Delete empty line in file
我有一个文件,我可以添加和删除信息,但是在删除函数(这是一个 50 的数组)期间,没有任何信息的每一行(最多 50 标记)都被设置为空行。基本上,如果文件中有 3 个项目,则在以前没有添加任何内容的地方将有 47 行任何内容。所以我正在寻找绝对最简单的方法来浏览文件并删除任何空行。
最简单的方法会很好,因为我仍在学习C++并且还不了解很多高级功能。
bool removeFriend(string currentFriend)
{
bool successFail = false;
string friendArray[50];
int counter = 0;
string debtReason, name, amountOwed;
ofstream fout;
ifstream fin;
fin.open("moneyFriendsOweMe.txt");
if (fin.is_open())
{
while (isalpha(fin.peek()))
{
getline(fin, name);
if (name != currentFriend)
{
friendArray[counter] = name;
counter ++;
getline(fin, debtReason);
friendArray[counter] = debtReason;
counter ++;
getline(fin, amountOwed);
friendArray[counter] = amountOwed;
counter ++;
}
else
{
getline(fin, debtReason);
getline(fin, amountOwed);
successFail = true;
}
}
fin.close();
}
fout.open("moneyFriendsOweMe.txt");
if (fout.is_open())
{
for(int i = 0; i < 50; i++)
{
fout << friendArray[i];
fout << "n";
}
fout.close();
}
return successFail;
}
即使文件中什么都没有,您似乎也在向文件写出一些东西。 与其尝试删除空行,为什么不首先阻止将它们写出来呢? 您可以通过使用计数器来执行此操作,该计数器为未删除的朋友写出多少行,然后只写出这些行。 现在你正在写出整个50个阵列,即使你阅读不到50个朋友。 这会导致额外的行。 以下是仅写出所需行的代码:
bool removeFriend(string currentFriend)
{
bool successFail = false;
string friendArray[50];
int counter = 0;
string debtReason, name, amountOwed;
ofstream fout;
ifstream fin;
fin.open("moneyFriendsOweMe.txt");
if (fin.is_open())
{
while (isalpha(fin.peek()))
{
getline(fin, name);
if (name != currentFriend)
{
friendArray[counter++] = name;
getline(fin, debtReason);
friendArray[counter++] = debtReason;
getline(fin, amountOwed);
friendArray[counter++] = amountOwed;
}
else
{
getline(fin, debtReason);
getline(fin, amountOwed);
successFail = true;
}
}
fin.close();
}
// open and also clear the file so that only the lines you want get written to it
fout.open("moneyFriendsOweMe.txt", ios::out | ios::trunc );
if (fout.is_open())
{
for(int i = 0; i < counter; i++) // only write out the friends you want to keep in the file
{
fout << friendArray[i];
fout << "n";
}
fout.close();
}
return successFail;
}
您也可以在第一个 while 循环中写出文件,甚至没有第二个 for 循环。 那么你甚至不需要数组。它看起来像这样:
bool removeFriend(string currentFriend)
{
bool successFail = false;
string debtReason, name, amountOwed;
ofstream fout;
ifstream fin;
fin.open("moneyFriendsOweMe.txt");
// open and also clear the file so that only the lines you want get written to it
fout.open("moneyFriendsOweMe2.txt", ios::out | ios::trunc );
if (fin.is_open() && fout.is_open())
{
while (isalpha(fin.peek()))
{
getline(fin, name);
getline(fin, debtReason);
getline(fin, amountOwed);
if (name != currentFriend)
{
fout << name << endl << debtReason << endl << amountOwed << endl;
}
else
{
successFail = true;
}
}
fin.close();
fout.close();
}
return successFail;
}
相关文章:
- 如何在我从C++文件中读取后立即删除行
- 删除 XML 文件中的子节点C++
- 如何在输入新内容 c++ 之前删除文件中的所有内容
- 如何更新和删除文本文件中的特定逗号分隔值?C++
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 如何自动获取我的项目的路径并删除一些文件
- C++ VS 错误:<实验/文件系统>提供 std::experimental::文件系统的标头已被Microsoft弃用,将被删除
- 如果我删除指针,我的C++程序会意外删除系统文件吗?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- 文件删除(使用取消链接)与释放所有磁盘空间之间的延迟
- 重命名批处理文件(删除名称中的一些符号)(python/matlab/shell)
- 如何找到字符串并使用C 文件中的文件删除它
- 使用 DEF 文件删除 dll 导出的名称
- 关闭后等待文件删除
- c++中的文件删除
- 如何共享文件删除权限时,我打开一个文件ifstream
- 典型的Win32文件删除(IDropTarget::drop)实现
- 生成文件模式规则:循环生成文件.o < - 删除生成文件依赖项