在 scanf 中使用 [^ 符号的目的是什么? [英] What is the purpose of using the [^ notation in scanf?

查看:88
本文介绍了在 scanf 中使用 [^ 符号的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些代码,想知道原始开发人员在做什么.以下是使用此模式的简化程序:

I have run into some code and was wondering what the original developer was up to. Below is a simplified program using this pattern:

      #include <stdio.h>

      int main()  {     

      char title[80] = "mytitle";      
      char title2[80] = "mayataiatale";      
      char mystring[80]; 

      /* hugh ? */
      sscanf(title,"%[^a]",mystring);
      printf("%s\n",mystring); /* Output is "mytitle" */


      /* hugh ? */
      sscanf(title2,"%[^a]",mystring); /* Output is "m" */
      printf("%s\n",mystring);


      return 0;  
  }

scanf 手册页 有相关信息,但我无法阅读它.使用这种符号的目的是什么?它试图完成什么?

The man page for scanf has relevant information, but I'm having trouble reading it. What is the purpose of using this sort of notation? What is it trying to accomplish?

推荐答案

字符类的主要原因是 %s 表示法在第一个空格字符处停止,即使您指定字段长度,并且您经常不想它.在这种情况下,字符类表示法会非常有用.

The main reason for the character classes is so that the %s notation stops at the first white space character, even if you specify field lengths, and you quite often don't want it to. In that case, the character class notation can be extremely helpful.

考虑这段代码读取最多 10 个字符的行,丢弃任何多余的字符,但保留空格:

Consider this code to read a line of up to 10 characters, discarding any excess, but keeping spaces:

#include <ctype.h>
#include <stdio.h>

int main(void)
{
    char buffer[10+1] = "";
    int rc;
    while ((rc = scanf("%10[^\n]%*[^\n]", buffer)) >= 0)
    {
            int c = getchar();
            printf("rc = %d\n", rc);
            if (rc >= 0)
                    printf("buffer = <<%s>>\n", buffer);
            buffer[0] = '\0';
    }
    printf("rc = %d\n", rc);
    return(0);
}

这实际上是在 comp.lang.c.moderated(大约 2004 年 6 月)上讨论与 getline() 变体相关的示例代码.

This was actually example code for a discussion on comp.lang.c.moderated (circa June 2004) related to getline() variants.

至少有些混乱.第一个格式说明符 %10[^\n] 最多读取 10 个非换行符,并将它们与尾随的 null 一起分配给缓冲区.第二个格式说明符 %*[^\n] 包含赋值抑制字符 (*) 并从输入中读取零个或多个剩余的非换行符.当 scanf() 函数完成时,输入指向下一个换行符.循环体读取并打印该字符,以便在循环重新开始时,输入会查看下一行的开头.然后重复该过程.如果该行少于 10 个字符,那么这些字符将被复制到缓冲区,并且零个或多个非换行符"格式处理零个非换行符.

At least some confusion reigns. The first format specifier, %10[^\n], reads up to 10 non-newline characters and they are assigned to buffer, along with a trailing null. The second format specifier, %*[^\n] contains the assignment suppression character (*) and reads zero or more remaining non-newline characters from the input. When the scanf() function completes, the input is pointing at the next newline character. The body of the loop reads and prints that character, so that when the loop restarts, the input is looking at the start of the next line. The process then repeats. If the line is shorter than 10 characters, then those characters are copied to buffer, and the 'zero or more non-newlines' format processes zero non-newlines.

这篇关于在 scanf 中使用 [^ 符号的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆