C 在linux中将输出重定向到文件
我正在写一个c程序,它在树莓PI(Debian Wheezy)和调制解调器之间建立串行通信。该程序工作到目前为止,但我希望输出也被重定向到一个.txt或.csv文件C 在linux中将输出重定向到文件,c,linux,serial-communication,C,Linux,Serial Communication,我正在写一个c程序,它在树莓PI(Debian Wheezy)和调制解调器之间建立串行通信。该程序工作到目前为止,但我希望输出也被重定向到一个.txt或.csv文件 #include <stdlib.h> #include <string.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> #defi
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#define MODEM "/dev/ttyUSB0"
#define BAUDRATE B9600
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
struct termios old_stdio;
int tty_fd, flags;
unsigned char c='D';
tcgetattr(STDOUT_FILENO,&old_stdio);
printf("Starte bitte %s /dev/ttyUSB0\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
if((tty_fd = open(MODEM , O_RDWR | O_NONBLOCK)) == -1){
printf("Error while opening\n"); // Just if you want user interface error control
return -1;
}
cfsetospeed(&tio,BAUDRATE);
cfsetispeed(&tio,BAUDRATE); // baudrate is declarated above
tcsetattr(tty_fd,TCSANOW,&tio);
while (c!='q'){
if (read(tty_fd,&c,1)>0){
write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
printf("");
}
if (read(STDIN_FILENO,&c,1)>0){
write(tty_fd,&c,1);//if new data is available on the console, send it to serial port
printf("");
}
}
close(tty_fd);
tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义调制解调器“/dev/ttyUSB0”
#定义波特率B9600
int main(int argc,字符**argv)
{
结构术语;
结构termios stdio;
结构termios old_stdio;
int tty_fd,标志;
无符号字符c='D';
tcgetattr(标准文件号和旧标准文件号);
printf(“Starte bite%s/dev/ttyUSB0\n”,argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
标签的标准c_=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(标准文件号、标准命令和标准命令);
tcsetattr(STDOUT_文件号、TCSAFLUSH和stdio);
fcntl(STDIN_FILENO,F_SETFL,O_NONBLOCK);//使读取成为非阻塞
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_of Lag=0;
tio.c|cflag=CS8 | CREAD | CLOCAL;//8n1,有关更多信息,请参阅termios.h
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
如果((tty_fd=open(MODEM,O_RDWR | O_NONBLOCK))=-1{
printf(“打开时出错”\n”);//如果您想要用户界面错误控制
返回-1;
}
cfsetospeed(&tio,波特率);
cfsetispeed(&tio,波特率);//波特率在上面声明
tcsetattr(tty_fd、TCSANOW和tio);
而(c!='q'){
如果(读(tty_fd,&c,1)>0){
write(STDOUT_FILENO,&c,1);//如果串行端口上有新数据,请将其打印出来
printf(“”);
}
如果(读(STDIN_FILENO,&c,1)>0{
write(tty_fd,&c,1);//如果控制台上有新数据可用,则将其发送到串行端口
printf(“”);
}
}
关闭(tty_fd);
TceTattr(标准文件号、标准文件号和旧标准文件号);
返回退出成功;
}
希望有人能帮我为什么不在你的课程之外做呢 下面将把程序的(std)输出写入文件
foo.txt
:
./myprogram > foo.txt
如果您还想查看输出,请通过管道将其传输到tee
(这将把它的stdin写入stdout和一个文件)
如果您完全确定要在程序中执行所有这些操作,则可能需要编写自己的write
函数,该函数同时写入stdout
和一个文件
ssize_t mywrite(int fd, const void *buf, size_t count) {
write(fd, buf, count);
return write(STDOUT_FILENO, buf, count);
}
然后使用它,而不是
write
(请注意,您应该将打开文件的filedescriptor传递给mywrite
;它也将写入STDOUT)不清楚为什么不简单地open()
一个新路径并write()
,而不是写入STDOUT。如果这不是一个选项(在您的示例代码中,它似乎是一个选项,是最好的选项),您可以使用此伪代码执行运行时重定向
#include <unistd.h>
// first open the new output path
int new_out = open("/path/to/output", flags_as_needed);
// next, move the standard output path
int save_out = dup(STDOUT_FILENO);
close(STDOUT_FILENO);
// now copy the new output path to the standard output position
dup2(STDOUT_FILENO, new_out);
#包括
//首先打开新的输出路径
int new\u out=open(“/path/to/output”,根据需要标记);
//接下来,移动标准输出路径
int save_out=dup(标准输出文件号);
关闭(标准文件号);
//现在将新的输出路径复制到标准输出位置
dup2(标准输出文件号,新输出);
只需打开一个文件并使用其文件描述符:#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char block[1024]; //buffer of data
int out; //file deccriptor
int nread; //size of data
out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
write(out,block,nread);
exit(0);
}
#包括
#包括
#包括
#包括
int main()
{
字符块[1024];//数据缓冲区
int out;//文件描述符
int nread;//数据的大小
out=open(“file.out”,O|u WRONLY | O|u CREAT,S|u IRUSR | S|u IWUSR);
写入(输出、块、nread);
出口(0);
}
我已经试过了,但似乎不起作用。我应该在代码中的何处插入此项?很抱歉,我是编程新手,也许你能帮我。创建了一个新文件,但它不会将输出写入该文件。您会遇到此后续失败,因为传递给open()
(\u flags\u as\u所需)的标志不包括写入。例如,这组标志将起作用(只要您选择的路径没有其他问题):O|u CREAT | O|u WRONLY,S|u IRUSR | S|u IWUSR
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char block[1024]; //buffer of data
int out; //file deccriptor
int nread; //size of data
out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
write(out,block,nread);
exit(0);
}