0

I need to send a fomatted data to a tcp ip port in my c++/cli program.I have this code with no success

String^ data;
sprintf(data,"L,%02u%02u%02u%02u%02u%02u%03u,%lf %lf\n",rDateTime.uiYear, rDateTime.usiMonth, rDateTime.usiDay,
                rDateTime.usiHour, rDateTime.usiMinute, rDateTime.usiSec, 
                rDateTime.udiUSec / 1000,container[i].first,container[i].second);

I get the error *error C2664: 'sprintf' : cannot convert parameter 1 from 'System::String ^' to char **

I want to write it to a string variable std::string.

Can someone provide with some suggestions.If atleast I convert it to System::String^. I can convert it to the std:string using this C++/CLI Converting from System::String^ to std::string. But I dont know how to write the different datatypes to a string^ in c++/cli..

Community
  • 1
  • 1
ShivShambo
  • 523
  • 12
  • 29
  • 2
    `sprintf` would probably be what you're looking for if it's there in cli. – chris Jul 04 '12 at 03:45
  • Have you considered System::String::Format? – reuben Jul 04 '12 at 03:48
  • @chris.. I tried that before and i edited my question accordingly – ShivShambo Jul 04 '12 at 03:54
  • sprintf() is a native C library function, it requires a char[] as the first argument. A System::String is not a char[]. Either use a char[] and convert to String or use String::Format(). You ought to get up to speed on composite string formatting if you plan to write more C++/CLI code. – Hans Passant Jul 04 '12 at 03:59
  • @HansPassant Can I give some sample example code. I am just beginning my steps in c++/cli. – ShivShambo Jul 04 '12 at 04:52

4 Answers4

3

You need to declare a temperory variable with type char*. I am using a fixed array here for demonstration purpose. Since you may have a long string, I suggest you to look at _snprintf to avoid buffer overflow error.

After you get your string in char*, you can create a managed System::String using gcnew

char str[1024];

sprintf(str,"L,%02u%02u%02u%02u%02u%02u%03u,%lf %lf\n",rDateTime.uiYear, rDateTime.usiMonth, rDateTime.usiDay, 
            rDateTime.usiHour, rDateTime.usiMinute, rDateTime.usiSec,  
            rDateTime.udiUSec / 1000,container[i].first,container[i].second); 

System::String^ data = gcnew System::String(str); 
Console::WriteLine(data);
Harvey Kwok
  • 11,713
  • 6
  • 37
  • 59
1

The sprintf() function takes a char array (char *) as it's first argument. If you want to use it in this way, you need to first write into a char array, and then convert it into a string. I don't know about System::String^, but you can convert a char array into an std::string by a simple assigning like this:

char * data = new char[50];
sprintf(data, "Your text goes here");
std::string str = data;

Don't forget to allocate memory for the char array! if you forget it and write something like this:

char * data;
sprintf(data, "Your text goes here");

you are going to get an error. On the other hand, if std::string is suitable for you, you can format it directly using formatting manipulators

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
1

I realize that this is Not The Way To Do It if you are writing new managed code, but if you are porting unmanaged legacy C++ code that works with printf and const char*, there is no reason to rewrite the whole thing Just To Be Right.

This works for me and should safely work for any size string. I use it to wrap some functions that call Trace(...) to give flexibility of having a delegated Action deal with logging messages.

void TraceBase1(const char* prefix, const char* format, va_list argp) {
  if (Logger::m_logDelegate != nullptr) {

    System::String^ message;

    int count = _vscprintf(format,argp) + 1;
    char* buffer = new char[count ];
    try {
        _vsnprintf(buffer,count,format,argp);
        message =  gcnew System::String(buffer);
        Logger::m_logDelegate(message);
    }
    finally {
        delete[] buffer;
    }
  }
}
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
0

Have you looked at how sprintf works on your platform?

K. Brafford
  • 3,755
  • 2
  • 26
  • 30