简体   繁体   中英

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. 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 . ,输入将以该字符结尾,后跟

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM