简体   繁体   English

将printf数据写入c ++ / cli中的字符串变量

[英]writing printf data to a string variable in c++/cli

I need to send a fomatted data to a tcp ip port in my c++/cli program.I have this code with no success 我需要将格式化的数据发送到我的c ++ / cli程序中的tcp ip端口。我没有成功的这段代码

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 ** 我收到错误*错误C2664:'sprintf':无法将参数1从'System :: String ^'转换为char **

I want to write it to a string variable std::string . 我想将其写入字符串变量std :: string

Can someone provide with some suggestions.If atleast I convert it to System::String^ . 有人可以提供一些建议吗?如果至少我将其转换为System :: String ^ I can convert it to the std:string using this C++/CLI Converting from System::String^ to std::string . 我可以使用此C ++ / CLI将其从System :: String ^转换为std :: string,将其转换为std:string But I dont know how to write the different datatypes to a string^ in c++/cli.. 但是我不知道如何在c ++ / cli中将不同的数据类型写入字符串^。

You need to declare a temperory variable with type char* . 您需要使用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. 由于您的字符串可能很长,因此建议您查看_snprintf以避免缓冲区溢出错误。

After you get your string in char* , you can create a managed System::String using gcnew char*获取字符串后,可以使用gcnew创建托管System::String

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);

The sprintf() function takes a char array ( char * ) as it's first argument. sprintf()函数将char数组( char * )作为其第一个参数。 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. 如果要以这种方式使用它,则需要先写入char数组,然后将其转换为字符串。 I don't know about System::String^ , but you can convert a char array into an std::string by a simple assigning like this: 我不知道System::String^ ,但是您可以通过以下简单分配将char数组转换为std::string

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! 不要忘记为char数组分配内存! 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 另一方面,如果std :: string适合您,则可以使用格式化操纵器直接对其进行格式化

您是否看过sprintf在您的平台上如何工作?

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. 我意识到如果您正在编写新的托管代码,那么这并不是解决问题的方法,但是如果您要移植可与printfconst char*一起使用的非托管旧版C ++代码,则没有理由重写整个事情。 。

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. 我用它包装了一些调用Trace(...)的函数,以使委派的Action处理日志消息具有灵活性。

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;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM