hi goodday! im having trouble in my code by checking does the file exist?
let say i already save a txt file, then i want to check if that file exists.
void addbook(); is addding a file.
void chkbook(); is my problem i cant check it in a way i input the filename not declaring is like this:
ifstream my_file("test.txt");//manually input the filename
if (my_file.good())
{
// read away
}
#include <iostream>
#include <fstream>
#include <string>
// name of the file is name_of_book + ".txt"
// first line in the file contains the name of the author
// second line in the file contains the name of the book
bool is_book( const std::string& name_of_book )
{
std::ifstream file( name_of_book + ".txt" ) ;
if( !file.is_open() ) returnfalse ; // failed to open file
// return true if there are two lines of text in the file,
// and the second line matches for the name of the book
std::string line ;
return std::getline( file, line ) && // successfully read the first line
std::getline( file, line ) && // successfully read the second line
line == name_of_book ; // second line is the name of the book
}
void chkbook()
{
std::cout << "Enter name of the book: \n" ;
std::string book_name ;
std::getline( std::cin, book_name ) ;
if( is_book(book_name) ) std::cout << "found book\n" ;
else std::cout << "book was not found\n" ;
}
#include <iostream>
#include<fstream>
#include<string>
usingnamespace std;
void addbook();
void chkbook();
int main()
{
int choice;
cout<<"************************************************************************************"<<endl;
cout<<"* Welcome to the Library *"<<endl;
cout<<"* *"<<endl;
cout<<"* *"<<endl;
cout<<"************************************************************************************"<<endl;
cout<<" How can we help you? "<<endl;
cout<<" 1. Add Book "<<endl;
cout<<" 2. Check Book "<<endl;
cin>>choice;
switch (choice) {
case 1:
addbook();
break;
case 2:
chkbook();
break;
default:
cout << '\a';
}
cin.ignore();
cin.get();
}
void addbook()
{
string add,add2;
char author[200],book[200];
cout<<"Enter the name of the book: ";
cin>>add;
cin.ignore();
cin.get();
add2=add;
add += ".txt"; // important to create .txt file.
ofstream createFile;
createFile.open(add.c_str(), ios::app);
cout << "Writing to the file" << endl;
cout << "Enter name of Author: ";
cin.getline(author, 100);
// write inputted data into the file.
createFile <<"Author: "<< author << endl;
cout << "Full name of the book: ";
cin.getline(book, 100);
createFile <<"Book: "<< book << endl;
createFile.close();
}
bool is_book( const std::string& name_of_book )
{
std::ifstream file( name_of_book + ".txt" ) ;
if( !file.is_open() ) returnfalse ; // failed to open file
// return true if there are two lines of text in the file,
// and the second line matches for the name of the book
std::string line ;
return std::getline( file, line ) && // successfully read the first line
std::getline( file, line ) && // successfully read the second line
line == name_of_book ; // second line is the name of the book
}
void chkbook()
{
std::cout << "Enter name of the book: \n" ;
std::string book_name ;
std::getline( std::cin, book_name ) ;
if( is_book(book_name) ) std::cout << "found book\n" ;
else std::cout << "book was not found\n" ;
}
Note: by default, your GNU compiler assumes that the program is written in a home-grown linux dialect of C++ based on the obsoleted 1998 C++ standard. If standard C++ is what is required, you need to tell it at least twice:
first with -std=c++11
and then a second time with -pedantic-errors
thanks it runs now but when i enter the name of the book, it says book was not found.
************************************************************************************
* Welcome to the Library *
* *
* *
************************************************************************************
How can we help you?
1. Add Book
2. Check Book
2
Enter name of the book:
math1
book was not found
Process returned 0 (0x0) execution time : 9.625 s
Press any key to continue.
#include <iostream>
#include <fstream>
#include <string>
void addbook();
void chkbook();
int main()
{
int choice;
std::cout<<"************************************************************************************\n" ;
std::cout<<"* Welcome to the Library *\n" ;
std::cout<<"* *\n" ;
std::cout<<"* *\n" ;
std::cout<<"************************************************************************************\n" ;
std::cout<<" How can we help you? \n" ;
std::cout<<" 1. Add Book \n" ;
std::cout<<" 2. Check Book \n" ;
std::cin >> choice;
// thrw away the new line left in the buffer after reading choice
std::cin.ignore( 1000000, '\n' ) ;
switch (choice)
{
case 1:
addbook();
break;
case 2:
chkbook();
break;
default: ;
}
std::cout << "press enter to quit ..." ;
std::cin.get() ;
}
void addbook()
{
std::cout<<"Enter the name of the book: ";
std::string book_name ;
std::getline( std::cin, book_name ) ;
std::cout<<"Enter the name of the author: ";
std::string author ;
std::getline( std::cin, author ) ;
std::ofstream( ( book_name + ".txt" ).c_str() ) << author << '\n' << book_name << '\n' ;
}
bool is_book( const std::string& name_of_book )
{
std::ifstream file( name_of_book + ".txt" ) ;
if( !file.is_open() ) returnfalse ; // failed to open file
// return true if there are two lines of text in the file,
// and the second line matches for the name of the book
std::string line ;
return std::getline( file, line ) && // successfully read the first line
std::getline( file, line ) && // successfully read the second line
line == name_of_book ; // second line is the name of the book
}
void chkbook()
{
std::cout << "Enter name of the book: \n" ;
std::string book_name ;
std::getline( std::cin, book_name ) ;
if( is_book(book_name) ) std::cout << "found book\n" ;
else std::cout << "book was not found\n" ;
}