[Solved] c++ open file in one line


You can use the constructor to specify the filename:

ifstream inputFile("data.txt");

See the details for std::basic_ifstream (constructor).

explicit basic_ifstream( const char* filename,
std::ios_base::openmode mode = ios_base::in );


First, performs the same steps as the default constructor, then associates the stream with a file by calling rdbuf()->open(filename, mode | std::ios_base::in) (see std::basic_filebuf::open for the details on the effects of that call). If the open() call returns a null pointer, sets setstate(failbit)

solved c++ open file in one line