Why do you think you need to do that? You don't need to do that if you just want to use it to open a file (or directory). You only need the doubled backslashes in a string literal in order to represent a single backslash in the actual string. But the string you have already has the backslashes that you want, so you shouldn't need to double them.
If you really needed to double the backslashes, then maybe something like this:
In what sense is Java going to "read" it? From a text file? If so, I still don't think it would need doubled backslashes. Have you tried it as is? At any rate, I've shown you a way to do it if you really think it's needed.
string is pure c++
you need #include<string> for it, and a compiler that was made after, what, 1999?
does that fix the string problem?
String (caps) is something else, its nonstandard and probably some sort of hackery from the before <string> that you should avoid.
it should not fail or char arrays. show us something broken by <string>. did you remove <cstring> or <string.h> to put in <string>? That would break things, because you took it away...
I told you the easy way to solve this if you want to stick to C as well, in the other thread.
Yes, that's exactly what the System:: nonsense is. But a project should be able to use <string> (which is different from <string.h>) even if System is being used. The standard C++ string class is part of the std namespace, so you either need to do "using std::string;" or specify std::string instead of just string.
Perhaps instead of using some old VS6 project, you should just start from scratch with a minimum example to help you understand, and then you can build out from there.
#include <cstdlib>
#include <string>
#include <iostream>
std::string escape_backslashes(const std::string& path)
{
std::string out;
for (size_t i = 0; i < path.size(); i++)
{
out += path[i];
if (path[i] == '\\')
{
out += '\\';
}
}
return out;
}
int main()
{
constchar* env_var = "ComSpec"; // CHANGE THIS TO WHAT YOU NEED
constchar* path = getenv(env_var);
if (!path)
{
std::cout << "Error receiving environmental variable!\n";
return 1;
}
std::string escaped_folder = escape_backslashes(path);
std::cout << escaped_folder << '\n';
return 0;
}
For me, this will print
C:\\Windows\\system32\\cmd.exe
to standard out.
________________________________
I'm starting to get the feeling this one of those "xy problems", and you've gone down a rabbit hole of using system environmental variables instead of something a lot simpler. Hard to tell, because you aren't explaining the overall purpose of what you are trying to accomplish.
Edit: Also, you inconsiderately make everything more confusing by jumping around to different threads. You now have people trying to answer your same question in multiple threads. https://www.cplusplus.com/forum/beginner/276959/#msg1195337