How to I replace characters in a char[]

Mar 21, 2021 at 11:08pm
I have this

1
2
char* POnlinee;
POnlinee = getenv(transferFolder);  // this will result in  C:\temp\1 


Now I need to change POnlinee to c:\\temp\\1

How do I do that?
Mar 21, 2021 at 11:35pm
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:

1
2
3
    string online {getenv(transferFolder)};
    for (size_t pos = 0; (pos = online.find('\\', pos)) != online.npos; pos += 2)
        online.insert(pos, 1, '\\');

Last edited on Mar 21, 2021 at 11:42pm
Mar 21, 2021 at 11:47pm
I need to convert it, so that Java is able to read it.
Mar 21, 2021 at 11:51pm
class "System::String" has no member "find"

how do I fix that?
Mar 21, 2021 at 11:54pm
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.
Mar 22, 2021 at 12:02am
if I do it without, it will crash, I tried that already. (don't ask my why that program crashes on that)

I tried your code, but it doesn't know string, so had to change it to String

but it doesn't know the other commands. like .find and .insert

I tried adding

#include <string>
using namespace std;

but then the code fails on everything else in regards to chars and arrays.
Mar 22, 2021 at 12:09am
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.
Last edited on Mar 22, 2021 at 12:11am
Mar 22, 2021 at 12:15am
using VS Studio 2019 community edition. latest version with update.

I have #include<string> but no difference.

btw. i am doing this in the code for a DLL. in case it makes a difference.
Mar 22, 2021 at 12:16am
1
2
3
4
5
6
7
8
9
10
11
12
13
#define LARGESIZE 5000
#define SLEEPINT 1

#include <string>


using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Collections;
using namespace System::Runtime::InteropServices;


is what I have

string and string.h are under independences.
Last edited on Mar 22, 2021 at 12:18am
Mar 22, 2021 at 12:20am
Using jonnin's idea:

1
2
3
4
5
6
7
    char online[512], *p = online;
    for (char *env = getenv(transferFolder); *env; ++env)
    {
        if (*env == '\\') *p++ = '\\';
        *p++ = *env;
    }
    *p = '\0';


BTW, we don't know what language that is. There is no System, etc., in standard C++.
Presumably it's "managed" C++: https://en.wikipedia.org/wiki/Managed_Extensions_for_C%2B%2B
Last edited on Mar 22, 2021 at 12:21am
Mar 22, 2021 at 12:22am
that is not c++ ?

the project was converted from an old VS 6 package. I upgraded it to 2019 package
Maybe that's why i am having issues?
Mar 22, 2021 at 12:40am
that is not c++ ?

It's not standard C++. It has special extensions for Windows.
It's probably so-called C++/CLI: https://en.wikipedia.org/wiki/C%2B%2B/CLI
Mar 22, 2021 at 2:09am
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#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()
{
	const char* env_var = "ComSpec"; // CHANGE THIS TO WHAT YOU NEED
	const char* 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
Last edited on Mar 22, 2021 at 3:28am
Topic archived. No new replies allowed.