fstream.. checking if the file exist

Mar 25, 2017 at 3:05am
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
}



here's my code



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 #include <iostream>
#include<fstream>
#include<string>

using namespace 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();



}

void chkbook()
{
    string book;

    cout<<"Enter name of the book: "<<endl;
    cin>>book;
    cin.ignore();
    cin.get();
    book += ".txt";

    ifstream file(book,ios_base::out|ios_base::in);
    if(!file)
    {
        cout<<"true";

    }
    else
    {
        cout<<"not";
    }


}
Last edited on Mar 25, 2017 at 3:11am
Mar 25, 2017 at 3:26am
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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() ) return false ; // 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" ;
}
Mar 25, 2017 at 3:47am
im having an error tho..,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include<fstream>
#include<string>

using namespace 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() ) return false ; // 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" ;
}

Mar 25, 2017 at 3:49am
> im having an error tho..,

What is the text of the error message?
Mar 25, 2017 at 3:50am
||=== Build: Debug in lib (compiler: GNU GCC Compiler) ===|
C:\Users\noelt\Desktop\ICTP122\lib\libraryy.cpp||In function 'bool is_book(const string&)':|
C:\Users\noelt\Desktop\ICTP122\lib\libraryy.cpp|73|error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::basic_string<char>)'|
C:\Users\noelt\Desktop\ICTP122\lib\libraryy.cpp|73|note: candidates are:|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|470|note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|470|note: no known conversion for argument 1 from 'std::basic_string<char>' to 'const char*'|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|456|note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char; _Traits = std::char_traits<char>]|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|456|note: candidate expects 0 arguments, 1 provided|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|430|note: std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)|
C:\Program Files\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|430|note: no known conversion for argument 1 from 'std::basic_string<char>' to 'const std::basic_ifstream<char>&'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Mar 25, 2017 at 4:07am
Either enable C++11 support with the options -std=c++11 and -pedantic-errors

or modify line 73 to:
1
2
// std::ifstream file( name_of_book + ".txt" ) ;
std::ifstream file( ( name_of_book + ".txt" ).c_str() ) ;



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
Mar 25, 2017 at 5:10am
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.
Mar 25, 2017 at 5:41am
Try this (first add the book using option 1 and then check for the book):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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() ) return false ; // 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" ;
}
Topic archived. No new replies allowed.