首页 > 解决方案 > 在 C++ 中包含库 database.txt 文件时出错

问题描述

我正在制作一个图书馆管理系统,其中包括包含书籍和书籍分配信息的文本文件。但它显示一个错误。我有一个 database.txt 文件,其中给出了书籍的名称和书籍的分配。请帮忙...

我的代码是

    #include<iostream.h>
    #include<conio.h>
    #include<fstream.h>
    #include "C:\Users\Ritesh Singh Chauhan\Desktop\librarydatabase.txt"
    char name[20];
    char book[20];
    char person[20];
    void searchBook()
{
 ifstream infile;
infile.open("librarydatabase.txt");
cout<<"enter name of book";
cin>>name;
infile>>name;
 cout<<name<<endl;
 infile.close();

}
void addNewBook()
 {
 ofstream outfile;
   outfile.open("librarydatabase.txt");
   cout<<"enter name of book";
   cin>>book;
   outfile<<book<<endl;
   cout<<"successfully added";
   outfile.close();

 }
 void allotmentList()
 {
  cout<<"enter name of person";
  cin>>person;
 }
 void exitLib()
 {
 //exit();
  }
 void main() 
 {
 clrscr();
  int number;
  cout<<"press 1 to search book"<<" "<<"press 2 to add a new book"<<" " 
   <<"press 3 to allotment list by person"<<" "<<"press 4 to exit";
   cin>>number;
  switch(number)
   { 
   case 1: searchBook();
    break;
   case 2: addNewBook();
   break;
   case 3: allotmentList();
   break;
   case 4: exitLib();
   break;
   default: cout<<"press number between 1 to 4";
    }

    [code is showing error and an error message is given in image][1]}

标签: c++

解决方案


从代码中删除以下行

#include "C:\Users\Ritesh Singh Chauhan\Desktop\librarydatabase.txt"

并换行

infile.open("librarydatabase.txt");

infile.open("C:\\Users\\Ritesh Singh Chauhan\\Desktop\\librarydatabase.txt");

推荐阅读