我需要帮助,了解如何编写在Linux中使用C++进行删除行的代码。量:日期
电费:100:25jan2015
电话费:100:25jan2015
话费:50:25jan2015在C++中删除LINUX文本文件中的行
说明上市
我想这样做,以便当用户选择选项b删除用户输入的费用,并防止程序删除重复的数据用户将不得不输入金额和日期。谢谢。
和TEMP.TXT用于存储什么被重命名为Expense.txt所以如果话费价值100移除它会成为
电费之前没有删除:100:25jan2015
telephonebill :50:25jan2015void 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();
你忘了说你当前的代码在做什么 - 即你在'temp.txt'中得到了什么输出。 – 2015-01-21 07:59:00