假设我有一个char* str = "0123456789"并且我想要剪切第一个和最后三个字母并打印中间,这是最简单,最安全的方法吗?
现在的诀窍:要切割的部分和要打印的部分是可变大小的,所以我可以有一个非常长的char*,或者非常小的char*.
我正在尝试打印一些字符串,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终止符 如果可能的话,我想要一种方法来实现这个输出,而无需修改字符串.
可能吗?