2015-01-21 41 views
1

我需要帮助,了解如何编写在Linux中使用C++进行删除行的代码。量:日期
电费:100:25jan2015
电话费:100:25jan2015
话费:50:25jan2015在C++中删除LINUX文本文件中的行

我在文本文件如下

说明上市

我想这样做,以便当用户选择选项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(); 
+1

你忘了说你当前的代码在做什么 - 即你在'temp.txt'中得到了什么输出。 – 2015-01-21 07:59:00

回答

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"); 
0

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

你的代码中有几个逻辑错误,比如行:

Expenses >> expense >> amount >> date 

可能不会做你希望做什么,因为运营商>>不处理(默认)用字符“ :'作为分隔符或分隔符。

另外,不使用用于检查输入错误的变量(x),ifstream的的ofstream对象具有类似的功能:ifstream::goodifstream::failifstream::bad

但是你可以用户x的检查,如果任何修改是,如果不是你不需要的文件操作:

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

说,您的评论CON被readed:

X在开始时设置为0,因此,如果没有改变,就意味着有错误

其实这是错误的,也x == 0意味着条件texpense==expense从未成为现实。

Recomendations

  1. 你可以使用std::iftream::getline并更改分隔符:
  2. 您可以使用std::iftream::getline结合正则表达式和std::regex::search

我认为第一个更容易(如果:没有被空间包围),第二个更优雅,因为不太容易出错。你决定。