Qubc
@Qubc
Ненавижу полисемию.

Почему scanf_s("%[^3]", str) при считывании символа '3' просто останавливается на этом символе, а не переходит к следующему?

while (1) {
        char buffer [ 100 ] = { 0 };
        int check;
        check = fscanf_s ( stdin, "%[^3]", buffer , 100 ); // ABC3DE
        check = fscanf_s ( stdin, "%[^3]", buffer , 100 ); // ошибка чтения
}

Например, fgetc() читает символ и переходит к следующему.
fscanf() с "%[^3]", получается, просто зависает на символе, который указан в [set]. Почему так? В чем суть?
  • Вопрос задан
  • 94 просмотра
Решения вопроса 1
@dima20155
you don't choose c++. It chooses you
Потому что так реализовано:

[set]
matches a non-empty sequence of character from set of characters.
If the first character of the set is ^, then all characters not in the set are matched. If the set begins with ] or ^] then the ] character is also included into the set. It is implementation-defined whether the character - in the non-initial position in the scanset may be indicating a range, as in [0-9]. If width specifier is used, matches only up to width. Always stores a null character in addition to the characters matched (so the argument array must have room for at least width+1 characters)


Ссылка

Если вам не нужно "зависать" на символе, то используйте другой формат, например %c, %s (симолы/строки)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы