加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C删除文件中的文本行

发布时间:2020-12-16 05:04:18 所属栏目:百科 来源:网络整理
导读:我的c程序有问题…我的整个程序是学生姓名,成绩和年龄的数据库,当用户想要删除1名学生的数据时,我的功能有问题.这是代码: void deletestudentdata(){ string name,grade,tname; int age,x=0; // x - "counter" to check if user entered wrong name system(
我的c程序有问题…我的整个程序是学生姓名,成绩和年龄的数据库,当用户想要删除1名学生的数据时,我的功能有问题.这是代码:
void deletestudentdata()
{
    string name,grade,tname;
    int age,x=0; // x - "counter" to check if user entered wrong name

    system("cls");
    cout << "Enter name of the student you want to erase from database" << endl;
    cin >> tname;

    ifstream students("students.txt");
    ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete

    while(students >> name >> grade >> age)
    {
        if(tname!=name){ // if there are students with different name,input their data into temp file
            temp << name << ' ' << grade << ' ' << age << endl;
        }
        if(tname==name){ // if user entered correct name,x=1 for later output message that the user data has been deleted
            x=1;
        }
    }
    students.clear(); // clear eof and fail bits
    students.seekg(0,ios::beg);
    students.close();
    temp.close();
    remove("students.txt"); 
    rename("temp.txt","students.txt");
    if(x==0){ // x was set to 0 at start,so if it didn't change,it means user entered the wrong name
        cout << "There is no student with name you entered." << endl;
    }
    else{ // x is not 0,it means user entered the correct name,print message that students data has been deleted
        cout << "Student data has been deleted." << endl;
    }
}

它有效,但问题是我输入学生数据,当我想通过这个功能删除它时它不会删除它,我首先必须关闭程序,然后重新打开程序,然后调用该功能,以便删除学生数据.

如何更改它以便我可以在输入后立即删除学生数据,而无需先关闭程序?

解决方法

像这样.它的eaiser显示代码而不是解释.
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;




void displaystudentdata()
{
   string name,x=0; // x - "counter" to check if user entered wrong name

    system("cls");

    ifstream students("students.txt");


    cout<<"-------------------------------------------------------------------nn";
    while(students >> name >> grade >> age)
    {

        cout<<"Name= "<<name <<",Grade= "<< grade <<",Age= " <<age<<"n";
    }
    students.clear(); // clear eof and fail bits
    students.seekg(0,ios::beg);
     students.close();
}

void deletestudentdata()
{
    string name,x=0; // x - "counter" to check if user entered wrong name



    ifstream students("students.txt");
    ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete


    cout<<"-------------------------------------------------------------------nn";

    cout << "Enter name of the student you want to erase from database >" << endl;
    cin >> tname;

    //ifstream students("students.txt");
    //ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete

    while(students >> name >> grade >> age)
    {
        if(tname!=name){ // if there are students with different name,print message that students data has been deleted
        cout << "Student data has been deleted." << endl;
    }
}


int main(void)
{



  displaystudentdata();
  deletestudentdata();
  displaystudentdata();
  cout << "Student data has been deleted. nn" << endl;
  cout<<" nPress any key to continuen";
  cin.ignore();
  cin.get();

   return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读