- 论坛徽章:
- 1
|
请高手赐教scanf的一个用法!谢谢
这个 我用scanf也没有写出来~ 我用IO终端控制给你写了一个.
- #include <stdio.h>;
- #include <error.h>;
- #include <termios.h>;
- #include <string.h>;
- #include <stdlib.h>;
- #include <sys/types.h>;
- int main()
- {
- char *tname,tempc,*cmdtemp,cmd[128];
- FILE *fp;
- int fd,len;
- struct termios term,termsave;
- tname=ctermid(NULL);
- if((fp=fopen(tname,"r+"))==NULL)
- {
- perror("fopen");
- exit(0);
- }
- setbuf(fp,NULL);
- fd=fileno(fp);
- tcgetattr(fd,&termsave);
- term=termsave;
- /*
- */
- term.c_lflag&=~(ECHO|ICANON|IEXTEN|ISIG);
- term.c_iflag&=~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
- term.c_cflag&=~(CSIZE|PARENB);
- term.c_cflag|=CS8;
- term.c_oflag&=~(OPOST);
- term.c_cc[VMIN]=1;
- term.c_cc[VTIME]=0;
- /*
- */
- memset(cmd,0,sizeof(cmd));
- cmdtemp=cmd;
- len=0;
- if(tcsetattr(fd,TCSAFLUSH,&term)<0)
- {
- perror("tcsetattr");
- exit(1);
- }
- while((tempc=getchar())!='\r')
- {
- if(tempc!='\t'&&tempc!=127&&tempc!=27)
- {
- *cmdtemp++=tempc;
- tcsetattr(fd,TCSAFLUSH,&termsave);
- printf("%c",tempc);
- tcsetattr(fd,TCSAFLUSH,&term);
- len++;
- }
- else if(tempc==27)
- {
- printf("\a");
- }
- else if(tempc==127)
- {
- if(len>;0)
- {
- *cmdtemp--=0;
- len--;
- printf("\b \b");
- }
- else if(len==0)
- {
- tcsetattr(fd,TCSAFLUSH,&termsave);
- printf("\a");
- tcsetattr(fd,TCSAFLUSH,&term);
- }
- }
- }
- *cmdtemp=0;
- tcsetattr(fd,TCSAFLUSH,&termsave);
- if(len!=0)
- {
- printf("%s\n",cmd); //这里就是你想控制的比如strcmp showdir函数什么的.
- }
- else
- {
- printf("hahaha\n"); //如果直接回车 就显示hahaha了.
- }
- }
复制代码 |
|