tom.c

来自「用五个实例来解释linux下的c编程」· C语言 代码 · 共 42 行

C
42
字号
/* tom.c v1.0 */#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#define FILE "message.txt"#define PROMPT "Please input a message: "#define SIZ (sizeof(PROMPT)-1)int main(){  int fd;  char buf[BUFSIZ];  ssize_t s;  /* Prompt user to input message. */  s = write(STDOUT_FILENO, PROMPT, SIZ);  if (s < SIZ) {    perror("write");    exit(EXIT_FAILURE);  }  /* Read message. */  s = read(STDIN_FILENO, &buf, BUFSIZ);  /* Open file for write. */  fd = open(FILE, O_CREAT | O_TRUNC | O_WRONLY, 0666);  if(fd < 0) {    perror("open");  }  /* Write message. */  s = write(fd, &buf, s);  /* Close file. */  close(fd);  exit(EXIT_SUCCESS);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?