2010年9月6日星期一

scanf的一个小技巧

形式:scanf("%...")
含义:
————————————————————————————————————————
默认:

meng@ubuntu:~/exam_0906$ cat -n test.c
     1 #include    "stdio.h"
     2 #define MIN(x,y)    ((x)<(y)?(x):(y))
     3
     4 int main(int argc, char *argv[])
     5 {
     6    char str[]="test input and out put .include space,table,enter... ";
     7
     8    printf("%s\n",str);
     9    printf("please input a srting:\n");
    10    scanf("%s",str);
    11    printf("The string you input are:%s\n",str);
    12    return 0;
    13 }

1:输入hello world,输出hello

meng@ubuntu:~/exam_0906$ ./test
test input and out put .include space,table,enter...
please input a srting:
hello world
The string you input are:hello


2:输入如googl(制表符)baidu,输出google

emeng@ubuntu:~/exam_0906$ ./test
test input and out put .include space,table,enter...
please input a srting:
google baidu
The string you input are:google

—————————————————————————————————————————

%[...]
这种形式的作用是只读取出现在中括号中的字符,如果读取的字符没有出现在这个中括号内,则停止读取
4 int main(int argc, char *argv[])
     5 {
     6     char str[]="test input and out put .include space,table,enter... ";
     7 
     8     printf("%s\n",str);
     9     printf("please input a srting:\n");
    10     scanf("%[a-z]",str); //匹配a-z
    11     printf("The string you input are:%s\n",str);
    12     return 0;
    13 }

meng@ubuntu:~/exam_0906$ ./test test input and out put .include space,table,enter... please input a srting: hello,world The string you input are:hello //遇到符号,停止读取

please input a srting:
gsghAgdf
The string you input are:gsgh   //遇到大写A停止读取

____________________________________________________________________________________________
%[^...]
('\n'。也就是说只要没有遇到换行就继续输入,当遇到换行符的时候此语句结束。而默认情况是遇到换行语句执行结束)
这种形式的作用是读取字符直到读取的字符是括号中^之后的任意一个字符为止
______________________________________________________________________________________________
%[^\n]

读取一个字符串,保存到后面对应的字符数组中,在读取字符串的过程中,遇到'\n'字符时就停止读取
     4 int main(int argc, char *argv[])
     5 {
     6     char str[]="test input and out put .include space,table,enter... ";
     7 
     8     printf("%s\n",str);
     9     printf("please input a srting:\n");
    10     scanf("%[^\n]",str);
    11     printf("The string you input are:%s\n",str);
    12     return 0;
    13 }
meng@ubuntu:~/exam_0906$ ./test
test input and out put .include space,table,enter... 
please input a srting:
hello world welcome-to-Beijing`
The string you input are:hello world welcome-to-Beijing`
//输入的字符串中包含:空格,制表符,符号,均可正常输出,直到遇到\n

meng@ubuntu:~/exam_0906$ ./test
     4 int main(int argc, char *argv[])
     5 {
     6     char str[]="test input and out put .include space,table,enter... ";
     7 
     8     printf("%s\n",str);
     9     printf("please input a srting:\n");
    10     scanf("%[^\t]",str);
    11     printf("The string you input are:%s\n",str);
    12     return 0;
    13 }

test input and out put .include space,table,enter... 
please input a srting:
sghg sgasd gg
sggdga gh gdh 
The string you input are:sghg sgasd gg
sggdga gh gdh
//输入一串字符,包含 回车,都可以原样输出,知道遇到制表符\t
————————————————————————————————————————

1 条评论: