如何在 Linux 中使用 _ultoa_s 和 _gcvt_s(使用 C++)?

问题描述 投票:0回答:1

我尝试通过以下代码将数字转换为字符串:

#include <iostream>

using namespace std;

int main()
{
    unsigned short a(3);
    float b(233.233);
    char a_string[100];
    char b_string[100];

    _ultoa_s(a,a_string,sizeof(a_string),10);
    _gcvt_s(b_string,sizeof(b_string),b,8);

    cout << a_string << endl;
    cout << b_string << endl;
    return 0;
}

这段代码在我使用windows时编译并运行良好,但在我使用Linux(我使用gcc编译器)时编译失败。 我收到以下错误消息:

file_5.cpp: In function ‘int main()’:
file_5.cpp:12:44: error: ‘_ultoa_s’ was not declared in this scope
     _ultoa_s(a,a_string,sizeof(a_string),10);
                                            ^
file_5.cpp:13:42: error: ‘_gcvt_s’ was not declared in this scope
     _gcvt_s(b_string,sizeof(b_string),b,8);
                                          ^

这部分代码的目的是将数字转换为字符串,稍后我将能够将其写入文件或将其用作文件名,因此如果您有其他想法如何以其他方式进行,它也有帮助。

有人能帮帮我吗?谢谢!

c++ linux type-conversion char
1个回答
0
投票

这些是特定于特定专有操作系统的供应商特定的非便携式功能。不要使用它们。

相反,请查看 std::to_string

 标题中的标准化,因此可移植的 
<string>

    unsigned short const a = 3;
    float const b = 233.233;
    std::string const a_string = std::to_string(a);
    std::string const b_string = std::to_string(b);
© www.soinside.com 2019 - 2024. All rights reserved.