写入(2)不会打印到标准输出

时间:2018-01-24 09:33:24

标签: c

#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <poll.h>
#include <string.h>
#include <errno.h>

#define BUFSIZE 256

char buffer[BUFSIZE];
int readBytes;
struct termios saved;
struct termios modif;

void onExit(){
  if(tcsetattr(STDIN_FILENO, TCSANOW, &saved) == -1)
    readBytes = readBytes; //placeholder
}

void nonCanonMode(){
  if(tcgetattr(STDIN_FILENO, &saved) == -1)
    readBytes = readBytes;
  if(tcgetattr(STDIN_FILENO, &modif) == -1)
    readBytes = readBytes;
  modif.c_iflag = ISTRIP;
  modif.c_oflag = 0;
  modif.c_lflag = 0;
  if(tcsetattr(STDIN_FILENO, TCSANOW, &modif) == -1)
    readBytes = readBytes;
  atexit(onExit);
}
void readChar(){
  int counter = 0;
  readBytes = read(STDIN_FILENO, buffer, BUFSIZE);
  if(readBytes == -1)
    readBytes = readBytes;
  while(counter < readBytes){
    if(buffer[counter] == '\004'){
      exit(0);
    }
    else{
      if(write(STDOUT_FILENO, &buffer[counter], sizeof(char)) == -1)
        readBytes = readBytes;
      write(STDOUT_FILENO, &buffer[counter], sizeof(char));
    }
  counter++;
  }
}
int main(){
  nonCanonMode();
  while(1)
    readChar();
 exit(0);
}

readChar()函数在键入字符时不打印字符。我将终端设置为非规范模式并无限循环此函数以一次读取一个字符。但是该函数只写入stdout一次。如果我注释掉第二个write那么就没有输出。

0 个答案:

没有答案