C++ 函数删除一行

c++ function deleting a line

本文关键字:一行 函数 删除 C++      更新时间:2023-10-16

大家好,当用户输入书籍ID时,我正在尝试从".txt"文件中删除一整行

我的文本文件看起来像这样:

[图书信息]%图书 ID : 1%标题 : 面向对象编程%[出版商 INFO]%发布者名称 : misr publish house%puplisher 地址 :adfaf%[作者信息]%作者姓名 : 艾哈迈德%国籍 : 埃及人%作者姓名 : eter%国籍 : 美国人%[更多 信息]%发布于 : 3/3/2006%状态 :6.

[图书信息]%图书 ID : 2%标题 : 自动化%[出版商信息]%出版商 姓名 : MISR%Puplisher 地址 :NASR City%[作者信息]%作者姓名 : 艾哈迈德·哈立德%国籍 : 埃及人%作者姓名 : 奥哈迈德 adel%国籍 : 埃及人%[更多信息]%发布于 : 3/8/2005%状态 :15.

该行从 [书籍信息] 到 (.) 开始当用户输入 id 时,我应该能够删除整行但我不知道如何使用或使用什么功能

我的代码是:

/*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 << "nnn";
    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 << "nnn";
    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 << "nn" << setw(76) << "{ welcome to FCIS library }nn";
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 << "nnn"<<setw(76) << "Thanks for Using FCIS LIBRARY" << endl;
        break;
    }
    else
        cout << "nwrong choice please choose againnn";
} while (_getch()!=27);
}

我尝试使用获取行并搜索匹配的 ID,如果有匹配项,请删除该行,但无法完成顺便说一下,我是C ++的初学者

将整个文件读入内存缓冲区。删除不需要的内容。用内存缓冲区的内容覆盖现有文件。您现在已经从文件中删除了您不需要的内容。