用的fscanf字符类 [英] fscanf with character classes

查看:144
本文介绍了用的fscanf字符类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个文件dog.txt

Say I have a file dog.txt

The quick brown fox jumps over the lazy dog.

我可以从这样的文件中读取

I can read from the file like this

# include <stdio.h>
int main(){
  char str[10];
  FILE *fp;
  fp = fopen("dog.txt", "r");
  fscanf(fp, "%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]", str);
  printf("%s\n", str);
  return 0;
}

和程序将输出 T 。 。但是不是列出所有的信件,我可以利用POSIX字符类,像 [:上:]

and the program will output T. However instead of listing all the letters, can I utilize the POSIX Character Classes, something like [:upper:] ?

推荐答案

没有,有没有可移植的方式来做到这一点。一些实现允许你使用字符范围像%[A-Z] ,但是这不是由C标准保证。 C99§7.19.6.2/ 12表示,这对 [转换符(强调):

No, there's no portable way to do it. Some implementations allow you to use character ranges like %[A-Z], but that's not guaranteed by the C standard. C99 §7.19.6.2/12 says this about the [ conversion specifier (emphasis added):

在转换说明包括在格式的所有后续字符字符串,直至并包括匹配的右括号(] )。方括号之间的字符(扫描列表的)构成了扫描集,除非左括号后的字符为抑扬( ^ ),在这种情况下,扫描集包含不出现在回旋,右括号之间的扫描列表中的所有字符。如果在转换说明始于 [] [^] ,右边括号字符是在扫描列表和下一下面的右括号字符表示结束规范匹配的右括号;否则其后的第一个右括号字符表示结束规范之一。 如果一个 - 字符是在扫描列表和其中第一个字符是不是第一个,也不是第二个是 ^ ,也不是最后一个字符,该行为是实现定义的。

The conversion specifier includes all subsequent characters in the format string, up to and including the matching right bracket (]). The characters between the brackets (the scanlist) compose the scanset, unless the character after the left bracket is a circumflex (^), in which case the scanset contains all characters that do not appear in the scanlist between the circumflex and the right bracket. If the conversion specifier begins with [] or [^], the right bracket character is in the scanlist and the next following right bracket character is the matching right bracket that ends the specification; otherwise the first following right bracket character is the one that ends the specification. If a - character is in the scanlist and is not the first, nor the second where the first character is a ^, nor the last character, the behavior is implementation-defined.

借助 POSIX.1-2008说明具有几乎相同的措辞(并使用POSIX系统时甚至推迟到ISO C标准在发生意外冲突的情况下),所以没有额外的保障在这种情况下。

The POSIX.1-2008 description has almost identical wording (and even defers to the ISO C standard in case of accidental conflict), so there are no additional guarantees in this case when using a POSIX system.

这篇关于用的fscanf字符类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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