convert char to string

This may seem a very basic thing to do, but so far I can't achieve it.
I need to convert a char array (effectively a char *) to a string to be able to read a folder using a DirectoryInfo structure.
Effectively I have a static main directory and a variable sub-directory that a concatenated together. However the DirectoryInfo structure needs a string.
I have tried simple assignment 'str = chr', a loop for all the chars in the char *, neither of these have worked.
I have searched this and other forums for solutions to this problem, but they all seem to give compilation errors.
Oh, I'm using Microsoft Visual C++ 2010 express.
Can anyone help?
You can assign a character array or a pointer to char to an object of type std::string. So I do not understand what is your problem.
Last edited on
The problem is that DirectoryInfo seems to take an argument of type string^ (according to Visual C++) and I don't seem to be able to do this conversion.
Im not sure i get your question but does this help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
#include <string>

int main( void )
{
     std::string emptyString = "";
     char arrayOfStuff[100];

     // fill in your array here


     // assign the values in the array to a string
     for( int index = 0; index < arrayOfStuff.size(); ++index ) // size in this case is 100
     {
            emptyString[index] = arrayOfStuff[index];
            // emptyString.c_str()[index] = arrayOfStuff[index];
     }

     return 0;
}


this isn't really complete and probably has errors but i don't have a compiler to check. You might not need to use .c_str() becuase im pretty sure you can access the individual elements of a string simply by using an index. if this isnt the answer you were looking for, could you post some code or restate your question so more people can add their ideas as well.

hope this helps,
What language are you actually trying to use? C++, C#, C++/CLI?

There is no such construction as string ^ in C++. It looks like you are trying to use C++/CLI environment.
I'm with Vlad on this one. why cant you just do:

1
2
3
4
	char* charVar = "wibble";	
	std::string stdStringVar(charVar);

// and pass stdStringVar into your DirectoryInfo? 
just use std::string constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;

int main()
{
	char q;

	char asd[] = "adas das das 232";

	string dsa = string(asd);

	cout << dsa.c_str() << endl;

	cin >> q;
	return 0;
}
1
2
3
4
5
6
7
char hello[] = "hello " ;
System::String^ a = gcnew System::String(hello) ;

System::Char world[] = L"world" ; 
System::String^ b = gcnew System::String(world) ;

Console::WriteLine( a + L' ' + b );

Yea, if that 'hat' symbol ('^') is not a typo, you're doing C++/CLI.
What exactly are you trying to do?
closed account (z05DSL3A)
norman1312 wrote:
The problem is that DirectoryInfo seems to take an argument of type string^ (according to Visual C++) and I don't seem to be able to do this conversion.


1
2
3
4
5
6
7
8
9
10
11
12
using namespace System;

int main(array<System::String ^> ^args)
{
    const char* charstr = "Hello, world!";

    System::String^ clistr = gcnew System::String(charstr);

    Console::WriteLine(clistr);

    return 0;
}
C++/CLI

Edit
More code for the heck of it.
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
using namespace System;
using namespace System::IO;

int main(array<System::String ^> ^args)
{
    const char* charstr = "c:\\temp";

    DirectoryInfo^ di = gcnew DirectoryInfo( gcnew System::String(charstr) );

    try
    {
        if ( di->Exists )
        {
            Console::WriteLine( "That path exists already." );
        }
        else
        {
            Console::WriteLine( "That path does not exists." );
        }
    }
    catch ( Exception^ e ) 
    {
        Console::WriteLine( "The process failed: {0}", e );
    }

    return 0;
}
C++/CLI
Last edited on
Read the example below. I think it will be of some help

#include <iostream>

using namespace std;

int main()
{
char string[256]; // A nice long string

cout<<"Please enter a long string: ";
cin.getline ( string, 256, '\n' ); // Input goes into string
cout<<"Your long string was: "<< string <<endl;
cin.get();
}

Actually vlad is correct it is C++/CLI.
If this board supported attachments I could show what some of the errors look like. I will have to cut/paste some of the compilation errors these various options throw up.
Watch this space as it may take a little time to get them all together.
The above approach is good enough when you have to input the value. But if you already have a value then using string_variable.c_str() is the best way to convert a character to a string.
There will now be quite a long pause while I read and digest the article and try out it's content and that of the contributors above.
Thanks, guys!
Topic archived. No new replies allowed.