【发布时间】:2015-05-15 13:40:24
【问题描述】:
我正在尝试使用 printf() 打印一些字符串,但它们 null 终止 带有尾随换行符,并且格式混乱:
printf("The string \"%s\" was written onto the file \"%s\"", str, fname);
假设字符串包含"The Racing car.",文件名为"RandomText1.txt"
这打印:
The string "The Racing car.
" was written onto the file "RandomText1.txt
"
但是我希望它只打印一行:
The string "The Racing car." was written onto the file "RandomText1.txt"
我知道我可以修改字符串以摆脱 null 终止符 换行符,但如果可能的话,我想在不修改字符串的情况下实现此输出。
有可能吗?
【问题讨论】:
-
“空终止符”?您不是指包含的换行符吗?
-
它们不是以null结尾的,它们的末尾有一个直线字符
-
抱歉,我是编程新手。我认为 \n 是空终止符
-
@ForceBru 我认为您的话有点具有误导性,它们是空终止的,并且在空终止之前有一个
\n,恕我直言:-) -
@SouravGhosh,是的,你当然是对的。我的意思是,空终止不是这里的问题。
标签: c string printf null-terminated