Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, guys!
I'm doing this, to see if one day I can print my arrays:
C++
// Case_2.cpp
//
#include <stdafx.h>
#include <string>
#include <iostream>
#include <fstream>
#include <istream>
#include "Form1.h"
#include <marshal_cppstd.h> // Also tried "marshal_cppstd.h"

using namespace Case_2;
//
int main ()
{
	void save_arrays ();
	return 0;
}
//
void save_arrays ()
{
   array<String^>^ my_array = gcnew array<String^> {"xxx", "yyy", "zzz"};
   for (int i = 0; i< 3; i++)
   {
      System::String^ clrString = my_array[i];
      std::string stdString = marshal_as<std::string>(clrString);
      std::cout << stdString << std::endl;
   }
   return;
}

ANY HELP?
Posted
Updated 1-Mar-12 19:29pm
v3

Except if you have a good reason to do otherwise, you should use wide strings.

That is, you should use wstring and wcout.

Conversion will be easier and the code will properly works with characters that are not available in ANSI charset.
 
Share this answer
 
Comments
Wilmer Lameda 1-Mar-12 21:34pm    
I'm gonna give it a try. Thanks.
I have made a little change in your program to make it run by using visual studio 2010... Find the code snippet below:

XML
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <istream>
#include <msclr\marshal.h>
using namespace System;
using namespace Runtime::InteropServices;

void save_arrays();
int main(array<System::String ^> ^args)
{
    save_arrays();
    return 0;
}

void save_arrays ()
{
    array<String^>^ my_array = gcnew array<String^> {"xxx", "yyy", "zzz"};
    for (int i = 0; i< 3; i++)
    {
        System::String^ clrString = my_array[i];
        const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(clrString)).ToPointer();
        std::string stdString = chars;
        std::cout << stdString << std::endl;
    }
    return;
}
 
Share this answer
 
Comments
Wilmer Lameda 2-Mar-12 5:28am    
Thank you. I´ll give it a shot. Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900