相关疑难解决方法(0)

在C中打印char []的一部分的最简单方法

假设我有一个char* str = "0123456789"并且我想要剪切第一个和最后三个字母并打印中间,这是最简单,最安全的方法吗?

现在的诀窍:要切割的部分和要打印的部分是可变大小的,所以我可以有一个非常长的char*,或者非常小的char*.

c string-formatting

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

如何使用printf打印字符串而不打印尾部换行符

我正在尝试打印一些字符串,printf()但它们是null终止 有尾随换行符和混乱的格式化:

printf("The string \"%s\" was written onto the file \"%s\"", str, fname);
Run Code Online (Sandbox Code Playgroud)

假设字符串包含"The Racing car.",文件名是"RandomText1.txt" This prints:

The string "The Racing car.
" was written onto the file "RandomText1.txt
"
Run Code Online (Sandbox Code Playgroud)

但是我希望它只用一行打印:

The string "The Racing car." was written onto the file "RandomText1.txt"
Run Code Online (Sandbox Code Playgroud)

我知道我可以修改字符串来摆脱它 null终止符 如果可能的话,我想要一种方法来实现这个输出,而无需修改字符串.

可能吗?

c string printf null-terminated

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

标签 统计

c ×2

null-terminated ×1

printf ×1

string ×1

string-formatting ×1