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

Recommended Answers

All 2 Replies

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);

thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.