简体   繁体   中英

c++ function deleting a line

hello guys i'm trying to delete a full line from a ".txt" file when the user enters the book id

my text file looks like this :

[BOOK INFO]%Book Id : 1%title : Object oriented programming%[PUBLISHER INFO]%publisher name : misr publish house%puplisher address :adfaf%[AUTHOR(s) INFO]%Authors Name : ahmed%Nationality : egyptian%Authors Name : eter%Nationality : american%[MORE INFO]%PublishedAt : 3/3/2006%status :6.

[BOOK INFO]%Book Id : 2%title : automate%[PUBLISHER INFO]%publisher name : misr%puplisher address :nasr city%[AUTHOR(s) INFO]%Authors Name : ahmed khaled%Nationality : egyptian%Authors Name : ohammed adel%Nationality : egyptian%[MORE INFO]%PublishedAt : 3/8/2005%status :15.

the line starts from [book info] to the (.) i should be able to delete the whole line when the user enter the id but i don't know how or what function to use

my code is :

/*password is admin*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include<stdlib.h>
#include<iomanip>
#include<conio.h>

#define F1      59
#define ESC     27

 using namespace std;

void list_all_books_1();
void list_available_books_2();
void borrow_books_3();
void search_for_abook_4();
void add_new_book_5();
void delete_books_6();

fstream d_base;
char path[] = "library books.txt";

void output(){
//this function for displaying choices only
cout << setw(77) << "***********************************" << endl;
cout << setw(71) << "1. List all books in library" << endl;
cout << setw(77) << "2. List available books to borrow " << endl;
cout << setw(72) << "3. Borrow a Book from library" << endl;
cout << setw(63) << "4. Search For a Book" << endl;
cout << setw(59) << "5. Add New Books" << endl;
cout << setw(59) << "6. Delete a Book" << endl;
cout << setw(62) << "7. EXIT The Library" << endl;
cout << setw(77) << "***********************************" << endl;
}

//=====================================================================================================================================================

 struct books{
//identfying books with all needed things
string id, status;
string title, p_name, p_address;
string date;
 };
struct author{
string aut_name;

string aut_nationality;
};

//=====================================================================================================================================================

//function for choice 1 showing the books

void list_all_books_1(){

ifstream show;
char all;
show.open(path, ios::in | ios::app);
while (!show.eof()){
    show >> std::noskipws;
    show >> all;
    if (all == '%')
        cout << "\n";

    else if (all == '.')
        cout << "\n\n\n";

    else
        cout << all;
}
cout << endl;
show.close();
}

 //=====================================================================================================================================================

 void list_available_books_2(){


ifstream available_books;
char x;
available_books.open(path, ios::in | ios::app);
while (!available_books.eof()){
    available_books >> std::noskipws;
    available_books >> x;
    if (x == '%')
        cout << "\n";

    else if (x == '.')
        cout << "\n\n\n";

    else
        cout << x;

}
cout << endl;
available_books.close();
 }

 //=====================================================================================================================================================

 void borrow_books_3(){

}

 //=====================================================================================================================================================

void search_for_abook_4(){

string idx;
int offset, i = 0;
string line;

cout << "enter the ID of the book you're looking for";
cin >> idx;

d_base.open(path, ios::in | ios::app);

while (!d_base.eof()){
    getline(d_base, line);
    if (((offset = line.find(idx, 0))) != string::npos){
        cout << "Book found" << endl;
        i++;
        d_base.close();
    }
}
if (i == 0){
    cout << "couldn't find book" << endl << endl;
}

}


//=====================================================================================================================================================

//for choice 5 to fill books
void add_new_book_5(){

int aut_number, booksnumber;
books newbook[1000];
author aut[100];
cout << "how many books you want to add ? ";
cin >> booksnumber;
cout << "what books you want to add :" << endl;
d_base.open(path, ios::out | ios::app);
for (int i = 0; i < booksnumber; i++){

    cout << "id please : "; cin >> newbook[i].id;
    cout << "title : ";              cin.ignore();   getline(cin, newbook[i].title);
    cout << "publisher name :";                      getline(cin, newbook[i].p_name);
    cout << "publisher address : ";                  getline(cin, newbook[i].p_address);

    d_base << "[BOOK INFO]" << "%Book Id : " << newbook[i].id << "%title : " << newbook[i].title;
    d_base << "%[PUBLISHER INFO]" << "%publisher name : " << newbook[i].p_name << "%puplisher address :" << newbook[i].p_address;
    d_base << "%[AUTHOR(s) INFO]";

    cout << "how many authors for the books";
    cin >> aut_number;

    for (int j = 0; j < aut_number; j++){
        cout << "author" << j + 1 << " name : ";    cin.ignore();    getline(cin, aut[j].aut_name);
        cout << "Nationality : ";                                    getline(cin, aut[j].aut_nationality);

        d_base << "% Authors Name : " << aut[j].aut_name << "% Nationality : " << aut[j].aut_nationality;
    }

    cout << "Publish date :";                                        getline(cin, newbook[i].date);
    cout << "How many copies of " << newbook[i].title << " ";       cin >> newbook[i].status;

    d_base << "%[MORE INFO]";
    d_base << "%PublishedAt : " << newbook[i].date << "%status :" << newbook[i].status << "." << endl;
}
d_base.close();
cout <<setw(76)<< "Books Have Been Saved Sucessfully" << endl;

}

//=====================================================================================================================================================

void delete_books_6(){
//deleting a book
}


//=====================================================================================================================================================

int main(){
char choice;

cout << "\n\n" << setw(76) << "{ welcome to FCIS library }\n\n";

do{
    output();
    cout << "- what do you want to do ? ";
    cin >> choice;

    if (choice == '1'){
        system("cls");

        list_all_books_1();
    }

    //this one for list available books
    else if (choice == '2'){
        system("cls");

        list_available_books_2();

    }

    //this one for borrow a book
    else if (choice == '3'){
        system("cls");

        borrow_books_3();

    }
    else if (choice == '4'){
        system("cls");

        search_for_abook_4();

    }

    //this one is for adding new books to the list
    else if (choice == '5'){
        system("cls");

        string pass;

        do{

            cout << "you must be an admin to add new books." << endl <<     "please enter passowrd (use small letters) : ";
            cin >> pass;

            if (pass == "b")
                break;

            else if (pass == "admin"){
                system("cls");

                cout << "ACCESS GAINED   WELCOME " << endl;

                add_new_book_5();
            }
            else{
                cout << "Wrong password try again or press (b) to try another choice";
                continue;
            }

        } while (pass != "admin");
    }

    //this one for deleteing a book
    else if (choice == '6'){
        system("cls");

        //not completed yet

    }
    else if (choice == '7'){
        system("cls");

        cout << "\n\n\n"<<setw(76) << "Thanks for Using FCIS LIBRARY" << endl;
        break;

    }
    else
        cout << "\nwrong choice please choose again\n\n";

} while (_getch()!=27);

}

i tried to use get line and search for matching id the delete the line if there's match but couldn't accomplish it i'm beginner at c++ by the way

Read the whole file into a memory buffer. Delete what you don't want. Overwrite the existing file with the contents of your memory buffer. You've now deleted what you did not want from the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM