convert char to string

Mar 19, 2013 at 12:02pm
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?
Mar 19, 2013 at 12:06pm
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 Mar 19, 2013 at 12:06pm
Mar 19, 2013 at 3:43pm
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.
Mar 19, 2013 at 3:48pm
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,
Mar 19, 2013 at 3:50pm
What language are you actually trying to use? C++, C#, C++/CLI?

Mar 19, 2013 at 3:52pm
There is no such construction as string ^ in C++. It looks like you are trying to use C++/CLI environment.
Mar 19, 2013 at 3:58pm
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? 
Mar 19, 2013 at 4:04pm
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;
}
Mar 19, 2013 at 4:14pm
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 );

Mar 19, 2013 at 4:14pm
Yea, if that 'hat' symbol ('^') is not a typo, you're doing C++/CLI.
What exactly are you trying to do?
Mar 19, 2013 at 4:14pm
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 Mar 19, 2013 at 4:22pm
Mar 19, 2013 at 4:17pm
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();
}

Mar 19, 2013 at 4:18pm
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.
Mar 19, 2013 at 4:19pm
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.
Mar 19, 2013 at 4:38pm
Mar 19, 2013 at 7:05pm
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.