我尝试删除文本文件中的行会留下回车
My attempt to delete a line in a text file is leaving a return carriage
在下面的函数中,我创建一个键,然后在输入文件中搜索它。该函数输出它读取的每一行,除非它与键匹配。
该函数似乎有效,只是在它复制最后一条记录后,endl
在文本文件上留下回车符。当我然后读取文件时,由于返回,它会读取最后一行两次。
我尝试使用空格,它做了完全相同的事情。有没有办法在 while 语句运行后我可以删除最后一个空格或返回?或者我可以避免写入空格或返回最后一条记录吗?
void deleteAddress(){
ifstream infile( "addressbook.txt", ios::in);
ofstream outfile ("ACopy.txt", ios::out);
string last, first, address1, address2, address3, address4;
string address5, address6, bday, anniversary, delL, delF;
int key, keyDel;
cout<<"Enter the name of the the client you want to delete (format: last, first)"<<endl;
cin>> delL >> delF;
keyDel = createKey(delL,delF);
while(! infile.eof()) {
(infile >> last >> first >> address1 >> address2 >>
address3 >> address4 >> address5 >> address6 >>
bday >> anniversary);
key = createKey(last, first);
if(key != keyDel) {
outfile << last << " "<<first << " "<<address1 << " " <<
address2 << " "<<address3 << " "<<address4 <<
" "<<address5 << " "<<address6 << " "<<bday <<
" "<<anniversary<<endl;
}
}
outfile.close();
infile.close();
}
以下是显示功能(displauCopy
)和我正在使用的createKey
:
void displayCopy(){
string line;
string last, first, address1, address2, address3;
string address4, address5, address6, bday, anniversary;
ifstream infile("ACopy.txt");
cout<<"First" << std::setfill(' ')<<setw(15) << "Last" <<endl;
while(! infile.eof())
{
(infile >> last >> first >> address1 >> address2 >>
address3 >> address4 >> address5 >> address6 >>
bday >> anniversary);
cout<< last <<", "<<first<<" "<<address1<<" "
<<address2<<" "<<address3<<", "<<address4<<", "
<<address5<<" "<<address6<<" "<<bday<<" "<<anniversary<<endl;
}
infile.close();
}
int createKey (string first, string last) {
int key;
key = (int)last[0]+(int)last[1]+(int)last[2]+(int)first[0];
return key;
}
问题是你正在循环 EOF 测试,这是一个坏主意。
你甚至不检查你的输入是否成功,所以即使它失败了(因为流没有错误,但里面什么都没有了),你仍然会输出一些东西。 由于这些字符串的作用域在循环之外,因此它们包含上次迭代放入其中的任何内容。
与其循环使用 EOF 测试,不如进行以下小修改:
while( infile >> last >> first >> address1 >> address2 >> address3
>> address4 >> address5 >> address6 >> bday >> anniversary )
{
cout<< last <<", "<<first<<" "<<address1<<" "
<<address2<<" "<<address3<<", "<<address4<<", "
<<address5<<" "<<address6<<" "<<bday<<" "<<anniversary<<endl;
}
或者,如果所有内容都在一行上,则可以使用 std::getline
然后从字符串流中读出:
int line_num = 0;
string line;
while( getline( infile, line ) )
{
++linenum;
istringstream iss( line );
if( iss >> last >> first >> address1 >> address2 >> address3
>> address4 >> address5 >> address6 >> bday >> anniversary )
{
cout<< last <<", "<<first<<" "<<address1<<" "
<<address2<<" "<<address3<<", "<<address4<<", "
<<address5<<" "<<address6<<" "<<bday<<" "<<anniversary<<endl;
}
else
{
cerr << "Error reading line " line_num << " : " << line << endl;
}
}
抱歉,我只是想出了解决这个问题的方法,我花了 2 个小时试图解决这个问题,发布后 5 分钟就想通
了void displayCopy2()
{
ifstream infile( "ACopy.txt", ios::in);
char readLetter;
char ch;
while((ch = infile.get()) != EOF)
{
cout<<ch;
}
}
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 目瞪口呆地寻找一种有效的 c 和 c++ 方法来删除回车符(读取.csv文件)
- 我尝试删除文本文件中的行会留下回车