LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2546|回复: 10

getch()的用法

[复制链接]
发表于 2003-5-27 17:45:54 | 显示全部楼层 |阅读模式
今天看了两位版主的回复。就开始重新写程序。但输入一个字母的时候还是会回显。又不想用system。就把getchar()改成getch();有加上#include <curses.h>
但编译时提示:
/tmp/ccidrEml.o: In function `main':
/tmp/ccidrEml.o(.text+0x9d): undefined reference to `stdscr'
/tmp/ccidrEml.o(.text+0xa2): undefined reference to `wgetch'
collect2: ld returned 1 exit status     
源代码为:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <curses.h>
  5. int
  6. main(void)
  7. {
  8.   char c;
  9.   int fd[2];
  10.   pid_t pid;

  11.   pipe(fd);
  12.   if((pid = fork()) == -1){
  13.     perror("frok error");
  14.     exit(-1);
  15.   }else if(pid == 0){
  16.     while(read(fd[0], &c, 1) > 0){
  17.       if(c == 'x')
  18.         break;
  19.       if(c >= 'a' && c <= 'z')
  20.         c -= 32;
  21.       write(STDOUT_FILENO, &c, 1);
  22.     }   
  23.   }else{
  24.     while([color=red]c = getch()[/color] ){
  25.       write(fd[1], &c, 1);
  26.       if(c == 'x')
  27.         break;
  28.       putchar(c);
  29.     }
  30.   }

  31.   exit(0);
  32. }
复制代码

好像curses.h的函数有一些限制,我用echo()也同样不行。我只是单独用一行写上echo()
发表于 2003-5-27 18:16:50 | 显示全部楼层
把你的要求说清楚,父进程要显示什么?子进程要显示什么?
最好给个你要求的输出。
发表于 2003-5-27 18:17:20 | 显示全部楼层
你不想回显的话就用noecho(),然后自己处理getch()得到的字符串。可以通过addch()把用户输入的字符显示到指定的位置。
发表于 2003-5-27 18:17:48 | 显示全部楼层
给你修改了一下:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <termios.h>
  5. int main(void)
  6. {
  7.     char c;
  8.     int fd[2];
  9.     pid_t pid;
  10.     struct termios initialsettings, newsettings; //initialsettings保存初始终端设置
  11.    
  12.     //读取当前终端设置并保存
  13.     tcgetattr( fileno(stdin), &initialsettings);
  14.    
  15.     //备份终端设置
  16.     newsettings=initialsettings;
  17.     //在newsettings中关闭echo
  18.     newsettings.c_lflag &= ~ECHO;
  19.     //用新参数对终端进行设置并检测返回值
  20.     if(tcsetattr(fileno(stdin), TCSAFLUSH, &newsettings) != 0) {
  21.             fprintf(stderr, "Could not set attributes\n");
  22.         }
  23.                     
  24.     pipe(fd);
  25.     if ((pid = fork()) == -1) {
  26.         perror("frok error");
  27.         exit(-1);
  28.     } else if (pid == 0) {
  29.         while (read(fd[0], &c, 1) > 0) {
  30.             if (c == 'x')
  31.                 break;
  32.             if (c >= 'a' && c <= 'z')
  33.                 c -= 32;
  34.             write(STDOUT_FILENO, &c, 1);
  35.         }
  36.     } else {
  37.         while (c = getchar()) {
  38.             write(fd[1], &c, 1);
  39.             if (c == 'x')
  40.                 break;
  41.             putchar(c);
  42.         }
  43.     }
  44.    
  45.     //恢复初始设置
  46.     tcsetattr(fileno(stdin), TCSANOW, &initialsettings);

  47.     exit(0);
  48. }
复制代码


编译时要加上-lcurses。 linux中要用getchar()函数。
下次粘贴代码时,请务必按置顶贴子的要求做。
发表于 2003-5-27 18:19:10 | 显示全部楼层
curses在有的发行版上面叫curses,有的叫ncurses。你编译的时候要加入 -lcurses
发表于 2003-5-27 18:25:47 | 显示全部楼层
最初由 libinary 发表
把你的要求说清楚,父进程要显示什么?子进程要显示什么?
最好给个你要求的输出。

程序很简单,就是一个管道,父进程从stdin读入一个字符,然后写入管道。子进程从管道中读入字符,转换成大写,输出到stdout。只是代码没有缩进,来得影响阅读。我用indent -kr美化一下,就比较好看了。
 楼主| 发表于 2003-5-29 20:34:30 | 显示全部楼层
最初由 Sworder 发表
curses在有的发行版上面叫curses,有的叫ncurses。你编译的时候要加入 -lcurses

我是有加上
#include <curses.h>
的.而且我用man -S2 noecho也能找到noecho函数.但不知道为什么.我一用getch()就会在编译时出现下面的提示:
/tmp/ccidrEml.o: In function `main':
/tmp/ccidrEml.o(.text+0x9d): undefined reference to `stdscr'
/tmp/ccidrEml.o(.text+0xa2): undefined reference to `wgetch'
collect2: ld returned 1 exit status
一用noecho()取消回显功能就会出现:
/tmp/ccWLYbfp.o: In function `main':
/tmp/ccWLYbfp.o(.text+0x1a): undefined reference to `noecho'
collect2: ld returned 1 exit status
我有在编译时加上-l curses.h
但提示找不到文件夹.
发表于 2003-5-29 21:46:08 | 显示全部楼层
在编译时加上的是-l curses,不是-lcurses.h。
 楼主| 发表于 2003-5-29 22:36:48 | 显示全部楼层
一样有试过的.
但找不到文件夹.估计是PATH的问题.
发表于 2003-5-29 23:17:39 | 显示全部楼层
在你的系统中安装了curses或者ncurses函数库了吗?看看/usr/lib下有没有libcurses.so之类的库文件。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表