开发者

Force scanf to consume newline

I have a program that reads data from stdin. This data is a sequence of bytes. If there is a byte describing a new line in it (in hex: 0x0A), scanf stops reading.

Can I mask this byte, so that scanf continues to read the whole sequence?

It is important that the memory, that is written开发者_如何学运维 by scanf contains the newline-byte.


Without seeing your code, I can't make a precise recommendation. But if your goal is take the input "as-is", I'll recommend read() as an alternative to scanf(). See this question for someone who had the exact opposite issue.


scanf("%[^`]s", str);

You can use some thing like this. `\n will now be the terminating sequence of characters.
You can replace ` using any other character or even a group of them and input will end with that character followed by a \n.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜