char* to string

How do you convert char* to a string. I've tried so many ways but I keep getting errors. I need convert them so i can combine the strings for a function.
1
2
3
	char *wd = _getcwd( NULL, 0 );
	char fPath[ sizeof( wd ) + 100 ];
	strncat_s( fPath, wd, sizeof( wd ) );
Wait, what exactly are you trying to do?
1
2
char cstring[] = "Blah blah";
std::string str(cstring); // Is this what you wanted? 
Does this work for what you want?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;
int main() {
   char *wd(getcwd(NULL,0));
   string cwd(wd);
   free(wd);
   cout << cwd << endl;
   return 0;
}
Last edited on
1) You have to give a buffer to getcwd. You can't give it 'NULL' or it will have nowhere to put the string data.

2) wd is a pointer. sizeof(a_pointer) gives you the size of a pointer (ie: probably 4 or 8 bytes, depending on your system), not the size of the data pointed to by the pointer.

3) strncat/strncat_s/strcat appends data to the end of an existing string. In this case, you are trying to append data to the end of fPath, but you never put anything in fPath (it's uninitialized) so there is no "end". You are potentially corrupting memory.



Do this:

1
2
3
4
5
6
7
8
9
char wd[256];  // use an actual buffer, not a pointer
getcwd( wd, 256 );  // give that buffer to getcwd and tell it how big the buffer is.

// if you want to put that data in a string...
string cwd = wd;  // like "long double main" suggested

// or if you want to copy it to a different char array (why?)
char wd2[256];
strcpy( wd2, wd );  // strcpy, not strcat 
Last edited on
Thanks, I didn't know the string constructor took char*. BTW _getcwd can take NULL. It allocates memory for the buffer automatically so that it is the smallest size possible and allows it to be bigger than maxpath.
It explains it in this example...
http://msdn.microsoft.com/en-us/library/sf98bd4y(v=vs.100).aspx

Here's the working code
1
2
3
	char *wdc = _getcwd( NULL, 0 );
	string wds( wdc );
	free( wdc );
Since the function that uses this later uses maxpath, i tried changing the code to use it, but now when i try to free memory i get an error.
1
2
3
4
	char wdc[ _MAX_PATH ];
	_getcwd( wdc, _MAX_PATH );
	string wds( wdc );
	free( wdc );


Windows has triggered a breakpoint in test.exe.

This may be due to a corruption of the heap, which indicates a bug in test.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while test.exe has focus.

The output window may have more diagnostic information.
Last edited on
man getcwd
getcwd() allocates the buffer dynamically using malloc(3) if buf is NULL.
Your buf is not NULL therefore the memory is not allocated. Your wdc is not from the heap so you can't free it.
Last edited on
Thanks, I guess I only need to free dynamic memory. I have one more queston.
How would you combine two buffers?
1
2
3
char *buffer1 = {"Buffer1"};
	char *buffer2 = {"Buffer2"};
	//char *buffer3 = {buffer1+buffer2}; 
With char arrays:
1
2
3
4
char buffer3[50]; // note buffer3 must be large enough to hold both strings + null terminator

strcpy( buffer3, buffer1 );  // copy buffer1 to buffer3
strcat( buffer3, buffer2 );  // append buffer2 to buffer3 


With strings:

 
string str3 = string(buffer1) + buffer2;
Last edited on
Topic archived. No new replies allowed.