相关疑难解决方法(0)

C++相当于sprintf?

我知道这std::cout是C++的等价物printf.

什么是C++等价物sprintf

c++ printf

70
推荐指数
5
解决办法
7万
查看次数

打印cout <<时的C++对齐方式

使用打印时有没有办法对齐文字std::cout?我正在使用标签,但是当单词太大时,它们将不再对齐.

Sales Report for September 15, 2010
Artist  Title   Price   Genre   Disc    Sale    Tax Cash
Merle   Blue    12.99   Country 4%  12.47   1.01    13.48
Richard Music   8.49    Classical   8%  7.81    0.66    8.47
Paula   Shut    8.49    Classical   8%  7.81    0.72    8.49
Run Code Online (Sandbox Code Playgroud)

c++ cout stdout text-alignment

60
推荐指数
6
解决办法
10万
查看次数

使用C++输出运算符打印前导零?

如何在C++中格式化输出?换句话说,什么是C++相当于使用printf这样的:

printf("%05d", zipCode);
Run Code Online (Sandbox Code Playgroud)

我知道我可以printf在C++中使用,但我更喜欢输出运算符<<.

你会使用以下内容吗?

std::cout << "ZIP code: " << sprintf("%05d", zipCode) << std::endl;
Run Code Online (Sandbox Code Playgroud)

c++ formatting numbers

54
推荐指数
4
解决办法
7万
查看次数

c array - warning:format不是字符串文字

我正在尝试学习C,但我已经遇到了问题.我认为它是微不足道的,但我需要知道它.我已经写了:

#include <stdio.h>
#include <string.h>

int main() 
{
    char str_a[20];

    strcpy(str_a, "Hello, world!\n");
    printf(str_a);
}
Run Code Online (Sandbox Code Playgroud)

一旦我尝试使用以下命令编译它:gcc -g -o char_array2 char_array2.c我收到错误说:

char_array2.c: In function ‘main’:
char_array2.c:9:2: warning: format not a string literal and no format arguments [-Wformat-security]
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

c compiler-warnings

40
推荐指数
3
解决办法
7万
查看次数

C++中的String.Format替代品

我没有太多使用C++的经验.相反,我在C#中工作得更多,所以,我想问一下我在那里会做些什么.我必须生成一个特定的字符串格式,我必须传递给另一个函数.在C#中,我可以通过以下简单代码轻松生成字符串.

string a = "test";
string b = "text.txt";
string c = "text1.txt";

String.Format("{0} {1} > {2}", a, b, c);
Run Code Online (Sandbox Code Playgroud)

通过生成这样一个上面的字符串,我应该能够通过它system().但是,system只接受char*

我在Win32 C++(不是C++/CLI),并且不能使用,boost因为它包含太多包含项目本身非常小的所有文件.喜欢的东西,sprintf()在我看来是有用的,但sprintf不接受stringa,bc参数.有什么建议我如何生成这些格式化的字符串以传递给我的程序中的系统?

c# c++ string string-formatting

28
推荐指数
4
解决办法
10万
查看次数

是否在C++ 11中写入&str [0] buffer(std:string)明确定义的行为?

char hello[] = "hello world";
std::string str;
str.resize(sizeof(hello)-1);
memcpy(&str[0], hello, sizeof(hello)-1);
Run Code Online (Sandbox Code Playgroud)

此代码是C++ 98中未定义的行为.它在C++ 11中是合法的吗?

c++ string buffer language-lawyer c++11

28
推荐指数
1
解决办法
2万
查看次数

设置回默认精度C++

我希望在比较期间控制double的精度,然后使用C++返回默认精度.

我打算用来setPrecision()设定精度.那么将精度设置回默认值的语法是什么(如果有的话)?

我正在做这样的事情

std::setPrecision(math.log10(m_FTOL));
Run Code Online (Sandbox Code Playgroud)

我做了一些事情,之后我想回到默认的双重比较.

我这样修改了,我还有一些错误

std::streamsize prec = std::ios_base::precision();
std::setprecision(cmath::log10(m_FTOL));
Run Code Online (Sandbox Code Playgroud)

cmath编译时为false,编译时为std::ios_basefalse.你能帮忙吗?

谢谢.

c++ floating-point precision default rounding

20
推荐指数
3
解决办法
3万
查看次数

如何使用参数集合格式化std :: string?

是否可以格式化std::string传递一组参数?

目前我正在以这种方式格式化字符串:

string helloString = "Hello %s and %s";
vector<string> tokens; //initialized vector of strings
const char* helloStringArr = helloString.c_str();
char output[1000];
sprintf_s(output, 1000, helloStringArr, tokens.at(0).c_str(), tokens.at(1).c_str());
Run Code Online (Sandbox Code Playgroud)

但是矢量的大小是在运行时确定的.是否有任何类似的函数sprintf_s采用参数集合并格式化std :: string/char*?我的开发环境是MS Visual C++ 2010 Express.

编辑: 我想实现类似的东西:

sprintf_s(output, 1000, helloStringArr, tokens);
Run Code Online (Sandbox Code Playgroud)

c++ string-formatting variadic-functions stdstring

15
推荐指数
2
解决办法
4万
查看次数

错误4错误C3861:'snprintf':找不到标识符

我正在通过visual studio 2008在win 7上运行一个程序

我收到此错误:

错误4错误C3861:'snprintf':找不到标识符

我已经包含了stdio标题...

我不知道还有什么可以遗漏

c++ printf visual-studio-2008 visual-c++

13
推荐指数
1
解决办法
4万
查看次数

在C++中使用带有std :: string的sprintf

sprintf在C++ 11中使用函数,方法如下:

std::string toString()
{
    std::string output;
    uint32_t strSize=512;
    do
    {
        output.reserve(strSize);
        int ret = sprintf(output.c_str(), "Type=%u Version=%u ContentType=%u contentFormatVersion=%u magic=%04x Seg=%u",
            INDEX_RECORD_TYPE_SERIALIZATION_HEADER,
            FORAMT_VERSION,
            contentType,
            contentFormatVersion,
            magic,
            segmentId);

        strSize *= 2;
    } while (ret < 0);

    return output;
}
Run Code Online (Sandbox Code Playgroud)

有没有比这更好的方法来检查每次保留的空间是否足够?为了将来添加更多东西的可能性.

c++ string c++11

12
推荐指数
3
解决办法
2万
查看次数