用C ++删除linux文本文件中的行

时间:2015-01-21 07:50:22

标签: c++

我需要帮助,我应该如何编写一个代码,用于在linux中使用c ++执行删除行。我在文本文件中的内容列在下面

  

描述:量:日期
  电费单:100:25jan2015
  电话费:100:25jan2015
  电话费:50:25jan2015

我想这样做,以便当用户选择选项b以移除用户输入的费用并防止程序删除重复数据时,用户必须输入金额和日期。谢谢。

并且temp.txt用于存储在重命名为Expense.txt之前未删除的内容,因此如果删除了电话帐单值100,它将变为

  

电费单:100:25jan2015
  telephonebill:50:25jan2015

void deleteexpensesdata(string& expense, string& texpense, double& amount,  string& date){
int check, x=0;
ifstream Expenses("Expense.txt");
ofstream temp("temp.txt");
cout << "\n";
cout << "Enter Type of Expense you wish to remove" << endl;
cin >> texpense;

while(Expenses >> expense >> amount >> date){
    if(texpense!=expense){//if there are expenses with different name, input their data into temp file
        temp << expense << ":" << amount << ":" << date <<endl;
    }
    if(texpense==expense){// if user entered correct name, x=1 for later output message that the user data has been deleted
       x=1; 
    }
}

Expenses.clear();
Expenses.seekg(0, ios::beg);
Expenses.close();
temp.close();
remove("Expense.txt");
rename("temp.txt","Expense.txt");
if(x==0){//x was set to 0 at start, so if it didn't change, it means there is error
    cout << "Remove of Expense failed" << endl;
}
else{//if x is not = 0 then remove is successful
    cout << "Expenses has been removed" << endl;
}
 }

调用该函数的代码在

之下
        cout << "Remove Expense.\n";
        deleteexpensesdata(expense, texpense, amount, date);
        cout << "Expense Date has been delete. \n" << endl;
        cin.ignore();
        cin.get();

2 个答案:

答案 0 :(得分:1)

下面的代码获取用户输入并将double转换为字符串并将所有3变量转换为分隔符“:”,如示例输入中所示,并且在打开文件后,获取不匹配的行临时文件并重命名该文件,基本上将其删除。

double expamt;
    string line, expdesc, exptrans;
    cout << "Please Enter Expense: \n";
    cin >> expdesc;
    cout << "Please Enter Amount: \n";
    cin >> expamt;
    string newamt = static_cast<ostringstream*>( &(ostringstream() << expamt) )->str();
    cout << "Date of Transaction: (e.g 20jan2015)\n";
    cin >> exptrans;

    string input = expdesc + ":" + newamt + ":" + exptrans;

    ifstream myfile;
    ofstream tempfile;
    myfile.open("Expense.txt");
    tempfile.open("temp.txt");
while (getline(myfile, line))
{
    if (line != input)
        tempfile << line;
}
    cout << "\nExpense " << expdesc << " has been removed \n" <<endl;
    myfile.close();
    tempfile.close();
    remove("Expense.txt");
    rename("temp.txt", "Expense.txt");

答案 1 :(得分:0)

首先建议:您应该学会使用gdb

您的代码有几个逻辑错误,例如行:

Expenses >> expense >> amount >> date

可能没有做您期望做的事情,因为运营商>>不会(默认情况下)处理字符&#39;:&#39;作为separtor或delimiter。

此外,请勿使用变量(x)来检查输入错误, ifstream ofstream 对象具有以下功能:{{3 },ifstream::goodifstream::fail

但是,您可以使用x检查是否有任何更改,如果不是,则不需要文件操作:

remove("Expense.txt");
rename("temp.txt","Expense.txt");

说,你的评论可以被提及:

  

x在开始时设置为0,所以如果它没有改变,则表示存在错误

实际上错了,x == 0也意味着条件texpense==expense永远不会成真。

<强> Recomendations

  1. 您可以使用ifstream::bad并将分隔符更改为:
  2. 您可以将std::iftream::getline与正则表达式和std::iftream::getline结合使用。
  3. 我认为第一个更容易(如果:没有被空格所包围),第二个更优雅,因为不容易出错。由你决定。