C++ 函数删除一行
c++ function deleting a line
大家好,当用户输入书籍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 ++的初学者
将整个文件读入内存缓冲区。删除不需要的内容。用内存缓冲区的内容覆盖现有文件。您现在已经从文件中删除了您不需要的内容。
相关文章:
- 如何使用运算符在同一行中多次调用函数
- uncrustify:如何将多行 C 函数调用的参数组合到一行上?
- 使用更改此变量的函数在同一行中打印引用变量
- Ofstream一直给我函数的最后一行,而不是显示所有内容
- C++将函数指针放入一行的映射中
- C++ 两个函数除了一行之外执行相同的操作
- 将地址返回到 2D 数组中的一行值的函数
- 无法理解代码的特定片段:这是一个函数,只是一行还是什么
- 线程1:EXC_BAD_ACCESS错误,此错误在第一行的CPP文件中的句柄函数中出现
- 当两个函数位于一行中时,堆栈框架的样子
- 如何将具有多行代码的内联函数视为一行?
- 为什么这个函数从不超出第一行
- 无法弄清楚为什么只从文件中读取一行矩阵以及为什么我无法将 2D 数组传递给函数
- clang format函数和错误检查在同一行
- 从函数结果中分配 std::shared_ptr 的一行是什么?
- 当const在另一行处的const时,doxygen parse const成员函数不正确
- 为什么在同一行上有 2 个函数调用时跳过函数调用
- 在文件中返回一行的函数
- getline函数只读取第一行
- C++ 函数删除一行