Why this should not work?
Why fstream doesn't accept a string as a path?Code:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
using namespace std;
string str = "C:\\test.del";
ifstream f(str);
}
Printable View
Why this should not work?
Why fstream doesn't accept a string as a path?Code:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
using namespace std;
string str = "C:\\test.del";
ifstream f(str);
}
fstreams take a const char* as the filename, so you have to call c_str() on the string to pass it to the fstream constructor or open function.
I know that, but it is wrong.Quote:
fstreams take a const char* as the filename, so you have to call c_str() on the string to pass it to the fstream constructor or open function.
You mean that you think they should change it? They might change it in the next standard. In the previous standard, fstreams were already widely used before the string class was created.
I agree that there should be an overload for strings, but I don't think its that big of a deal.
The much bigger problem with fstream is the lack of any way to provide a wchar_t pathname.
That means WinNT systems can't express pathnames in the native character set of their machine, and thus it's impossible in standard C++ to open all possible filenames (you need to use WinAPI file handling in lieu of C++ streams).
Of course they should change it. Because at least standard library should support itself. It is now contrary to what Stroustrup has in mind.Quote:
You mean that you think they should change it? They might change it in the next standard.