+ 2
[SOLVED] Using strings for file path
I`m trying to write a program in C++ that gets the file name from the user and save some info in a .txt file with the name that user wanted. but it gives me error. Can someone tell me what the problem is? string name, path = ".txt"; cin >> name; name += path; ofstream myFile; myFile.open(name); //ERROR
9 ответов
+ 4
Quoting from http://www.cplusplus.com/reference/fstream/ofstream/open/
* Signature of ofstream::open()
void open (const char* filename, ios_base::openmode mode = ios_base::out);
As we can see the first argument <filename> type is a `const char*`, not std::string.
This probably is the problem, maybe Dev C++ strictly accepts only `const char*` rather than reading the C-string inside the std::string <name> internal buffer e.g. through `c_str()` or `data()`.
Try and see if passing C-string works.
afile.open( name.c_str() );
+ 5
I don't use Dev C++ either Shadow, and I have no idea which standard version is adopted by Dev C++.
My prediction goes this way after I read the error message in OP's code.
"so only OP can test it"
Right 👍
+ 3
I executed your snippet and couldn't find anything wrong with it. I would guess that is not the entire code. Is it possible for you to link the entire code here (preferably as a playground project, not directly copy-pasted), maybe along with the error message the compiler issues?
+ 3
Ipang That only applies to C++98, which you can reasonably call outdated I think. If you open the C++11 tab, you can see there is an overload that should accept an std::string as first argument. Also, since C++17 there are additional overloads for std::filesystem not even mentioned there.
However, I agree it would be interesting to see whether DevC++ acceps the result of c_str() instead. According to the error message, no matching function signature is available for std::string, making me think something is wrong on the side of DevC++, but maybe this works as a workaround. Either way, I don't use the program, so only OP can test it.
+ 3
Actually, the following thread suggests it might use C++98 by default, although it dates back a few years:
https://stackoverflow.com/questions/16951376/how-to-change-mode-from-c98-mode-in-dev-c-to-a-mode-that-supports-c0x-ran
I'm too used to modern compilers, lol. When I started, C++17 was already a thing.
IliaAbbasi Try checking what standard version of C++ you compile your program with, according to the link I shared above. Ipang might be on the right track with this answer.
+ 3
Out of curiosity, did you check what standard version of C++ you compiled with in DevC++? I'm kind of interested in whether C++98 is actually still the default there. Because if it is, I would really change it to C++11, lol.
+ 2
what does the error say?
+ 2
Ipang, I really appreciate you man. It worked with no errors!
And thank you to everyone who helped me to solve my problem.
+ 1
thanks for the answers ...
https://code.sololearn.com/c9a22A16a15A/#cpp
This is the code.
not actually all of them because this little lines of code still has errors.
I use Dev-C++ maybe its because of the program.