以下代码导致错误并导致我的应用程序死亡.这是有道理的,因为缓冲区只有10个字节长,文本长度为22个字节(缓冲区溢出).
char buffer[10];
int length = sprintf_s( buffer, 10, "1234567890.1234567890." );
Run Code Online (Sandbox Code Playgroud)
我如何捕获此错误,以便我可以报告它而不是崩溃我的应用程序?
编辑:
阅读下面的评论后,我选择_snprintf_s.如果它返回-1值,则缓冲区未更新.
length = _snprintf_s( buffer, 10, 9, "123456789" );
printf( "1) Length=%d\n", length ); // Length == 9
length = _snprintf_s( buffer, 10, 9, "1234567890.1234567890." );
printf( "2) Length=%d\n", length ); // Length == -1
length = _snprintf_s( buffer, 10, 10, "1234567890.1234567890." );
printf( "3) Length=%d\n", length ); // Crash, it needs room for the NULL char
Run Code Online (Sandbox Code Playgroud) 以下C函数:
int sprintf ( char * str, const char * format, ... );
Run Code Online (Sandbox Code Playgroud)
将格式化数据写入字符串.作为str传递的数组的大小应该足以包含整个格式化的字符串.但是,如果格式化字符串的长度未提前知道怎么办?如何使用此函数(或类似其他函数)写入格式化数据,其长度未知?
例如:
#include <stdio.h>
int main ()
{
char buffer [13];
int n, a=5, b=3;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a %d char long string\n",buffer,n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
缓冲区需要为13或更大才能使其正常工作.如果字符串长度未知,并且缓冲区(例如)已设置为5,则此操作无效.我需要能够为碰巧大于缓冲区的字符串动态分配或重新分配缓冲区的东西.
我正在研究这个程序,我注意到使用%f表示double,%d表示float,这给了我一些完全不同的东西.谁知道为什么会这样?
int main ()
{
float a = 1F;
double b = 1;
printf("float =%d\ndouble= %f", a, b);
}
Run Code Online (Sandbox Code Playgroud)
这是输出
float = -1610612736
double = 190359837192766135921612671364749893774625551025007120912096639276776057269784974988808792093423962875123204096.0000
Run Code Online (Sandbox Code Playgroud) 当脚本进行时,如何让我的PowerShell脚本以表格格式打印信息.
在bash我会这样做
printf "%s\t%-15.15s" "Locale" "Jar"
if($verbose);then
printf "%-15.15s %-15.15s" "HelpSet" "Exception"
fi
printf "\t%s\n" "Status"
...
printf "%s\t%-15.15s" $locale $helpFileName
if($verbose); then
printf "%-15.15s %-15.15s" "$helpSetName" ${exclusion[$helpFileName]}
fi
status="OK"
...
if ($fixed); then
status="CORRECTED"
fi
printf "\t%s\n" $status
Run Code Online (Sandbox Code Playgroud)
要得到
Locale Jar HelpSet Exception Status
de help_D150 help_D150 CORRECTED
es help_D150 help_D150 OK
fr help_D150 help_D150 OK
it Locale folder not found
nl help_D150 help_D150 CORRECTED
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试将现有数据从MySQL数据库导出到我可以运行的ruby命令中rake db:seed.
这是我的代码.
# Generate db:seed data for proveedores.
$proveedores = R::findAll('tbproveedores');
$proveedoresE = R::exportAll($proveedores);
foreach ($proveedoresE as &$p) {
$line = 'BookSupplier.create(company: "%s", city: "%s", country: "%s", address: "%s", telephone: "%s", contact: "%s", email: "%s", website: "%s"\n';
$exportedLine = sprintf($line, $p['empresa'], $p['ciudad'], $p['pais'], $p['direccion'], $p['telefono'], $p['personacontacto'], $p['email'], $p['website']);
var_dump($exportedLine);
fwrite($seeds, $exportedLine);
echo "<br />";
}
Run Code Online (Sandbox Code Playgroud)
注意\n$ line变量末尾的换行符号.我在网上看到这就是使用新行所需要的全部内容.
我上面代码的输出是(vertabim,一条长行):
BookSupplier.create(company: "Pearson", city: "Lima", country: "Peru", address: "Av. Limon", telephone: "4673535421", contact: "Javier", email: "", …Run Code Online (Sandbox Code Playgroud) 所以我有一个字符串,它有一定的字节数(或长度).我说字节是因为字符串末尾没有NULL终止符.虽然,我知道字符串有多长.通常,正如我们所知,当你printf("%s", str);,它将继续打印每个字节,直到它变为NULL字符.我知道没有C字符串不是NULL终止,但我有一个奇怪的情况,我存储的东西(不是特定的字符串),我不存储NULL,但"事物"的长度.
这是一个小样本:
char* str = "Hello_World"; //Let's use our imagination and pretend this doesn't have a NULL terminator after the 'd' in World
long len = 5;
//Print the first 'len' bytes (or char's) of 'str'
Run Code Online (Sandbox Code Playgroud)
我知道你可以这样做:
printf("%.5s", str);
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,我很难对5进行编码,尽管在我的情况下,5是在变量中.我会做这样的事情:
printf("%.(%l)s", len, str);
Run Code Online (Sandbox Code Playgroud)
但我知道你做不到.但是让你知道我想要完成的事情.
我通过quora答案看到了这个程序
#include<stdio.h>
int main() {
printf("%d\n", ( { int n; scanf("%d", &n); n*n; } ));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是如何工作的,如果这符合标准?
也许这不属于SO,但我不知道在哪里.
我不得不重新实现printf(3)与C不使用,会做的转换对我有什么功能,我几乎做了,但我坚持上%a,我真的不明白发生了什么位置,例如:
printf("%a\n", 3.0); //#=> 0x1.8p+1
printf("%a\n", 3.1); //#=> 0x1.8cccccccccccdp+1
printf("%a\n", 3.2); //#=> 0x1.999999999999ap+1
printf("%a\n", 3.3); //#=> 0x1.a666666666666p+1
printf("%a\n", 3.4); //#=> 0x1.b333333333333p+1
printf("%a\n", 3.5); //#=> 0x1.cp+1
printf("%a\n", 3.6); //#=> 0x1.ccccccccccccdp+1
Run Code Online (Sandbox Code Playgroud)
我当然读到了那个说:
double参数被舍入并在样式[ - ] 0xh.hhhp [+ - ] d中转换为十六进制表示法,其中十六进制点字符后面的位数等于精度规范.
但是,这并不能真正帮助我不明白的过程是转换3.2到1.999999999999ap+1
我不需要任何代码,但更多的解释.
PS:如果这不是这个问题的地方,你可以指引我到正确的地方吗?
编辑:虽然@juhist答案适用于数字> = 1.0它没有解释如何获得0.0et 之间的数字结果1.0:
printf("%a\n", 0.01); //#=> 0x1.47ae147ae147bp-7
printf("%a\n", 0.1); //#=> 0x1.999999999999ap-4
printf("%a\n", 0.2); //#=> 0x1.999999999999ap-3
printf("%a\n", 0.3); //#=> 0x1.3333333333333p-2 …Run Code Online (Sandbox Code Playgroud) 我正在将文件类型数据读入一个bash数组,并需要在与空格相同的行上打印它的内容.
#!/bin/bash
filename=$1
declare -a myArray
readarray myArray < $1
echo "${myArray[@]}"
Run Code Online (Sandbox Code Playgroud)
我尝试这甚至与回声-n标志仍然打印在换行,我错过了什么,会更好地工作printf的?
我写了以下内容:
#include <stdlib.h>
#include <stdio.h>
void ExecAsRoot (char* str);
int main ()
{
printf ("Host real ip is:");
ExecAsRoot("ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'");
return 0;
}
void ExecAsRoot (char* str) {
system (str);
}
Run Code Online (Sandbox Code Playgroud)
我的预期输出是:
Host real ip is:7.17.11.29
Run Code Online (Sandbox Code Playgroud)
而实际输出是:
7.17.11.29
Host real ip is:
Run Code Online (Sandbox Code Playgroud)
为什么是这样?