Hi all,

I require some help using ifstream. I want to open a file who's name is made up in part by a user enter string - this is the format of the filename ...

"Airports_" << airlineICAO << ".txt"

i did try the following ...

ifstream airportsSource ("Airports_" << airlineICAO << ".txt");

How do i achieve this? Any help most appreciated.

Recommended Answers

All 3 Replies

I've also tried this with no luck ...

...

    string tempString1 = "Airports_";
    string tempString2 = airlineICAO;
    string tempString3 = ".txt";
    airportFileName = tempString1+tempString2+tempString3;

[B]    ifstream airportSource;
    airportSource.open(airportFileName);[/B]

/*    
    if (!airportsSource)
    { 
        cerr << "Could not open 'Airports_" << airlineICAO << ".txt' file!\n\n";
        system("PAUSE");
        exit (1);
    }

...

>airportSource.open(airportFileName);

ifstream::open() expects a C-style string as a parameter, not a C++ string. To convert a C++ string into a C-style string, use the c_str member function:

airportSource.open(airportFileName.c_str());

Thank you very much Joe !!!!!

I used

ifstream airportsSource(airportFileName.c_str());

works fine.

:)

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.