ifstream is not working

+1 ragmar thomas · September 13, 2014
#include <iostream>
#include <fstream>

using namespace std;

struct ddpl{
    string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
    long int isbn, shelf_no;
    char id[15], borrow_date[15], due_date[15];
};

void addAndSearch(){
    ddpl adding, sch;
    cout << "for adding -1" << endl;
    cout << "for searching -2" << endl;
    int chs;
    do{
        cout << "enter choice: ";
        cin >> chs;cin.ignore();

        if(chs==1){
            cout << "enter the Title of the book to add: " ;
            getline(cin, adding.title);

            ofstream added_books;
            added_books.open(adding.title.c_str(), ios_base::app);

            cout << "Author: ";
            getline(cin, adding.author);

            cout << "ISBN: ";
            cin >> adding.isbn;

            cout << "Shelf_No: ";
            cin >> adding.shelf_no;

            cout << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            cout << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;

            added_books << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            added_books << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;
            }
        else
            if(chs==2){
                ifstream read;
                read.open(adding.title.c_str());

                cout << "enter the title of the book to search: ";
                getline(cin, sch.show_title);

                if(adding.title==sch.show_title){
                    cout << "AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
                while(!read.eof()){
                        read >> adding.author;
                        read >> adding.isbn;
                        read >> adding.shelf_no;
                    cout << adding.author;
                    cout << adding.isbn ;
                    cout << adding.shelf_no;break;
                    }
                }else
                    if(adding.title!=adding.title)
                        cout << "the book is not found in the library." << endl;
                }
            else
                cout << "bye!!!" << endl;
    }while(chs!=0);
}
int main()
{
    addAndSearch();
}



this a fragment of code that i am working with, and it  only write to a data to the associated file name but it does not read from it. i tried any possible way that i know and i end up asking for a help and a better way to deal with it.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · September 13, 2014
The main problem was you tried to search for the book which you added. But if you close the console and restart he won't find the book
and vou need to use getline to read of file because the author can have a space in it. 

And I don't know if it's intenion but if you add the book two times it ads lines in the file, so you can have two books with the same name.
Then you need to use a loop on the search part.

And do you only want to search a book on its title?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct ddpl{
string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
long int isbn, shelf_no;
char id[15], borrow_date[15], due_date[15];
};

void addAndSearch(){
ddpl adding, sch;
cout << "for adding -1" << endl;
cout << "for searching -2" << endl;
int chs;
do{
cout << "enter choice: ";
cin >> chs; cin.ignore();

if (chs == 1){
cout << "enter the Title of the book to add: ";
getline(cin, adding.title);

ofstream added_books;
added_books.open(adding.title.c_str(), ios_base::app);

cout << "Author: ";
getline(cin, adding.author, '\n');

cout << "ISBN: ";
cin >> adding.isbn;

cout << "Shelf_No: ";
cin >> adding.shelf_no;

cout << "AUTHOR\t\tISBN\t\tSHELF_No" << endl;
cout << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;

added_books << "AUTHOR\t\tISBN\t\tSHELF_No" << endl;
added_books << adding.author << "\t\t" << adding.isbn << "\t\t" << adding.shelf_no << endl;
}
else
if (chs == 2){
ifstream read;
//read.open(adding.title.c_str());

cout << "enter the title of the book to search: ";
getline(cin, sch.show_title);

read.open(sch.show_title.c_str());
//if (adding.title == sch.show_title){
cout << "AUTHOR\t\tISBN\t\tSHELF_No" << endl;
//while (!read.eof()){
string buf;
getline(read, buf, '\n'); //for the AUTHOR\t\tISBN... line
getline(read, buf, '\t');
sch.author = buf;

getline(read, buf, '\t'); //for the \t\t
getline(read, buf, '\t');
sch.isbn = atoi(buf.c_str());

getline(read, buf, '\t'); //for the \t\t
getline(read, buf);
sch.shelf_no = atoi(buf.c_str());

cout << sch.author << "\t\t";
cout << sch.isbn << "\t\t";
cout << sch.shelf_no;
//break;
//}
cout << endl;
//}
/*else{
if (adding.title != adding.title)
cout << "the book is not found in the library." << endl;
}*/
}
else
cout << "bye!!!" << endl;
} while (chs != 0);
}
int main()
{
addAndSearch();
}
0 ragmar thomas · September 15, 2014
thanks, fro the reply.
i think these are the lines that are making work the ifstream. i just want to make sure.

sch.isbn = atoi(buf.c_str());
//
//
sch.Shelf_No = atoi(buf.c_str());


just to be sure what if the data type is not char let say it is a string does this function works the same way?
And i have googled that it is a function that convert each character to an integer by counting the length, and if does not work like this please correct me.

getline(read, buf, '\n');    //for the AUTHOR\t\tISBN... line

here the (read, buff) it means like read the whole buffer size of the string?! and if it is why do we need to use
otoi
+1 Franz Schmidt · September 15, 2014
Your first problem was this line:
if (adding.title == sch.show_title){

This line checks if adding.title is equal to search.title but if you close the compiler the adding title won't be set and if you add two books the first book also doesn't gets checked if it exists.


Then your next problem was:
read >> adding.author;
read >> adding.isbn;
read >> adding.shelf_no;

If you have a Author with more  than one name (e.g. Bucky Roberts) he read woutld stop at Bucky and Roberts would be in the next read for the isbn and the isbn would be the shelf.
Btw. you saved the values in the adding struct but in the search section I would use the search struct. It's not a misstake but it gets kinda confusing.
And another problem is that isbn and shelf_no are defined as integer so you can't "read" chars or a string.


Therefore we use the atoi() function. This function converts a text (chars or string) into a number (integer).
https://buckysroom.org/forum/topic.php?id=1701

The getline() function has the ability to read until the line ends or it gets to a character you declare. 
e.g.:
getline(read, buf, 'a');

This function reads until it hits an 'a' and saves the value in the buffer (string or char array).
If you don't declare the last value it reads until it hits a RETURN (\n).
0 ragmar thomas · September 19, 2014
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct ddpl{
    string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
    string isbn, shelf_no, id;
    //char id[15], borrow_date[15], due_date[15];
};

void addAndSearch(){
    ddpl adding, sch;
    string reader;
    cout << "for adding -1" << endl;
    cout << "for searching -2" << endl;
    int chs;
    do{
        cout << "enter choice: ";
        cin >> chs;cin.ignore();

        if(chs==1){
            ofstream added_books;
            added_books.open("DIC.txt", ios_base::app);

            cout << "Title: " ;
            getline(cin, adding.title);

            cout << "Author: ";
            getline(cin, adding.author);

            cout << "ISBN: ";

            cin >> adding.isbn;

            cout << "Shelf_No: ";
            cin >> adding.shelf_no;

            cout << "TITLE \t\t AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            cout << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;

            added_books << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;
            }
        else

            if(chs==2){
                ifstream read;
                read.open("DIC.txt");


                while(read.is_open() && !read.eof()){

                    getline(read, adding.title, ',');
                    getline(read, adding.author, ',');
                    getline(read, adding.isbn, ',');
                    getline(read, adding.shelf_no, '.');

                    cout << adding.title << "," << adding.author  << "," << adding.isbn << "," << adding.shelf_no << endl << endl;


                }}else
                    if(reader!=adding.title)
                        cout << "the book is not found in the library." << endl;

            else
                cout << "bye!!!" << endl;
    }while(chs!=0);

}
int main()
{
    addAndSearch();
}




@franz this works perfectly fine and prints all the file from the file  "DIC.txt" , and then i did this
it works for the first line only
does this thing have any thing to do with the code you gave me?
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct ddpl{
    string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;
    string isbn, shelf_no, id;
    //char id[15], borrow_date[15], due_date[15];
};

void addAndSearch(){
    ddpl adding, sch;
    string reader;
    cout << "for adding -1" << endl;
    cout << "for searching -2" << endl;
    int chs;
    do{
        cout << "enter choice: ";
        cin >> chs;cin.ignore();

        if(chs==1){
            ofstream added_books;
            added_books.open("DIC.txt", ios_base::app);

            cout << "Title: " ;
            getline(cin, adding.title);

            cout << "Author: ";
            getline(cin, adding.author);

            cout << "ISBN: ";

            cin >> adding.isbn;

            cout << "Shelf_No: ";
            cin >> adding.shelf_no;

            cout << "TITLE \t\t AUTHOR \t\t ISBN \t\t SHELF_No" << endl;
            cout << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;

            added_books << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl;
            }
        else

            if(chs==2){
                ifstream read;
                read.open("DIC.txt");


                while(read.is_open() && !read.eof()){

                    getline(read, adding.title, ',');
                    getline(read, adding.author, ',');
                    getline(read, adding.isbn, ',');
                    getline(read, adding.shelf_no, '.');

                    cout << "enter the title: ";
                    getline(cin, reader);

                    if(reader==adding.title)
                    cout << adding.title << "," << adding.author  << "," << adding.isbn << "," << adding.shelf_no << endl << endl;

                }}else
                    if(reader!=adding.title)
                        cout << "the book is not found in the library." << endl;

            else
                cout << "bye!!!" << endl;
    }while(chs!=0);

}
int main()
{
    addAndSearch();
}

and i want it to print the files in this way like printing all associated files in one line.
and the searching part that supposed to work with these line only works for the first line of the file


if(reader==adding.title)
0 Franz Schmidt · September 19, 2014
This is the modified search-part:
if (chs == 2){
ifstream read;
int counter = 0;
read.open("DIC.txt");

cout << "enter the title: ";
getline(cin, reader);

       while (read.is_open() && !read.eof()){

getline(read, adding.title, ',');
getline(read, adding.author, ',');
getline(read, adding.isbn, ',');
getline(read, adding.shelf_no, '\n');

if (reader == adding.title){
    cout << adding.title << "," << adding.author << "," << adding.isbn << "," << adding.shelf_no << endl << endl;
counter++;
}
}
if (counter > 0){
cout << counter << " Book(s) found!" << endl;
}
else{
cout << "the book is not found in the library." << endl;
}
}


I thinkyour main problem was 
getline(read, adding.shelf_no, '.');

The problem is you don't have any dots in your file where you could stop reading so you read the rest of your text in the file.

if (reader == adding.title){

This line is okay if these variables match you found the book in the library.

Btw. As you can see I added a little feature: If you have more than on book with the same name you also want to list them and so I added a counter for the found books. And obvious if the counter stays 0 you didn't find the book in the library.
0 ragmar thomas · September 22, 2014
Thanks dude i trolled a lot with out reading your advice. Thank u.
0 Franz Schmidt · September 22, 2014
No problem :D
  • 1

C++

106,942 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator