我尝试删除文本文件中的行会留下回车

My attempt to delete a line in a text file is leaving a return carriage

本文关键字:回车 删除 文本 文件      更新时间:2023-10-16

在下面的函数中,我创建一个键,然后在输入文件中搜索它。该函数输出它读取的每一行,除非它与键匹配。

该函数似乎有效,只是在它复制最后一条记录后,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;                
}
}