【问题标题】:Can't delete nextline after getline(cin , (variable)) in a txt file无法在 txt 文件中的 getline(cin , (variable)) 之后删除下一行
【发布时间】:2013-12-30 06:47:40
【问题描述】:

我正在编写一个简单的程序,用户可以:

A:添加新客户端
B:查看客户列表
C:删除数据库
D:删除客户端

它在大多数情况下运作良好,问题基本上是每个客户也有债务。 债务显示在客户姓名下方的行中。

Name: bryan
Debt: 45.56$

Name: Rosa
Debt: 23.43$

Name: Jon
Debt: 55.55$

我想这样当客户的名字被删除时,它下面的行也会被删除,等等。什么代码可以使这个工作?我只有大约 1 个月的时间进入 c++,所以如果你能把它简化一下让我理解,我将非常感激。 :)

我的代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std ;

int kill();
int end() ;
int read() ;
int append() ;
int main() 
{
  string input ;
  double cash ;
  cout << "A simple program to enter information about clients.\n" ;
  cout << "________________" << endl ;
  cout << "If you wish to add a new client, press A. \n" ;
  cout << "If you wish to see the list of existing clients, press B. \n" ;
  cout << "If you wish to delete this file, press C. \n" ;
  cout << "If you wish to delete a client, press D. \n" ;
  cout << "________________" << endl ;

  getline(cin, input ) ;
  cout << endl ;
  cout << endl ;
  cout << endl ;
  if(input == "A")
  {
    append() ;
  }
  if(input == "B")
  {
    read() ;
  }
  if(input == "C")
  {
    string input2;
    cout << "Are you sure you wish to delete the entire file? You will lose the 
      information.\n"
    cout <<"Enter C again, if you are sure: " ;
    getline(cin, input2);
    if(input2 == "C")
    {
     end();
    }

  }
  if(input == "D")
  {
    kill();
  }
  return 0 ;
}

int kill()
{
  string deleteline, nextline ;
  string line;
  ifstream kill; 
  kill.open("Clients.txt") ;
  ofstream temp ;
  temp.open("temp.txt") ;
  cout << "What Client do you wish to delete from the database?\n" ;
  getline(cin, deleteline) ;
  deleteline = "Name: " + deleteline;
  while (getline(kill,line))
  {
     if (line != deleteline )
     {
      temp << line << endl ;            
     }
  }
  temp.close();
  kill.close();
  remove("Clients.txt");
  rename("temp.txt","Clients.txt");
}


int append()
{
  string name ; 
  double cash ;

  ofstream writer("Clients.txt" , ios::app) ;
  cout << "Enter the client's name: " ;
  getline(cin, name) ;
  cout << "\n" ;
  cout << "Enter the client's debt balance: " ;
  cin >> cash ;
  cout << endl ;
  writer <<"Name: " << name << endl ;
  writer <<"Debt: ";  
  writer << cash << "$" << endl ;
  writer << "\n" ;
  writer.close() ;
}

int read()
{
  string line ;
  ifstream read("Clients.txt") ;
  if(read.is_open())
  {
    while(getline(read , line))
    {
        cout << line << "\n" ;
    }
    read.close() ;
  }
}

int end()
{
  ofstream ofs ;
  ofs.open("Clients.txt", ios::out | ios::trunc);   
  ofs.close() ;
}

主要问题在于我的 kill() 函数,它负责删除行,并在用户输入“D”时调用。

【问题讨论】:

    标签: c++ function ifstream getline ofstream


    【解决方案1】:

    读而不写:

    while (getline(kill,line))
    {
       if (line != deleteline )
       {
          temp << line << endl ;            
       }
       else
       {
          getline(kill, line);   // Error handling is pretty irrelevant here.
       }
    }
    

    【讨论】:

    • 哇,这行得通!对不起,伙计,你能详细说明你在这里做了什么吗?我复制了它,它可以工作,但我不太明白。 :(
    • 另外,我将如何删除它留下的空格?这样它就只在客户端之间留下一个空间?
    • 基本上,当您使用 getline 时,文件指针会连续遍历文件中的行。因此,通过执行上述操作,文件指针移动到新行而不会将换行符写入文件,并且下次它指向下一个客户端。
    • @BryanFajardo:在else 部分再次执行相同的操作以跳过空格。
    • 非常感谢你。我开始更好地理解它。 :)
    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2021-02-23
    • 2018-08-13
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多