0

hi guys,
i wonder how you can can take a string from a particular file and assign that to a variable.

for example here is the file named 'isbn.txt'

123-456-789-0
87-345-21-347
09-876-543-21

and my code, as far is i can write it:

int fromfile()
{
    string filename;
    ifstream fin;
    string isbn;
    
    cout << "enter the filename: ";
    cin >> filename;
    
    fin.open(filename.c_str());
    if (fin.fail())
    {
        cerr << "error. file not found" << endl;
        return 0;
    }    

    //isbn = ?????
    // i dunno how to assign isbn in the file to my isbn variable
   
}

I want to assign the isbn from my file (eg. 123-456-789-0)
to the isbn variable in the code.

so if anyone know how to do that, please help.

thank you

2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by kryptolite
0
std::string isbn;
getline(fin,isgm);

if you want to read them all into an array

std::vector<std::string> isbn_list;
std::string isbn;

while( fin >> isbn)
  isbn_list.push_back(isbn);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.