linux 打印输出重定向的问题
生活随笔
收集整理的這篇文章主要介紹了
linux 打印输出重定向的问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、應用場景:
當我們要調試一臺基于linux設備時,如果出現(xiàn)問題,但是沒有串口查看信息,這個時候就可以考慮使用telnet登入到設備,并用串口打印信息重定向到telent窗口的方案;
二、源碼實現(xiàn):
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/ioctl.h> #include <unistd.h>int main(int argc, char *argv[]) {int tty = -1;char *tty_name = NULL;if(argc < 2){printf("miss argument\n");return 0;}/* 獲取當前tty名稱 */tty_name = ttyname(STDOUT_FILENO);printf("tty_name: %s\n", tty_name);if(!strcmp(argv[1], "on")){/* 重定向console到當前tty */tty = open(tty_name, O_RDONLY | O_WRONLY);ioctl(tty, TIOCCONS);perror("ioctl TIOCCONS");}else if(!strcmp(argv[1], "off")){/* 恢復console */tty = open("/dev/console", O_RDONLY | O_WRONLY);ioctl(tty, TIOCCONS);perror("ioctl /dev/console");}else{printf("error argument\n");return 0;}close(tty);return 0; }?
總結
以上是生活随笔為你收集整理的linux 打印输出重定向的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt中rcc工具简介
- 下一篇: linux下tty, ttyn, pts