将输出重定向到linux中的文件

时间:2022-11-02 13:53:02

i am writing a c program, that sets up serial communication between a Raspberry PI (Debian Wheezy) and a modem. The program works so far but i want the output also to be redirected in a .txt or .csv file.

我正在编写一个程序,它在Raspberry PI(Debian Wheezy)和调制解调器之间建立串行通信。该程序到目前为止工作,但我希望输出也重定向到.txt或.csv文件。

#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; 
}

Hopefully there is one who can help me

希望有人可以帮助我

3 个解决方案

#1


1  

why not do that outside of your program?

为什么不在你的程序之外呢?

the following will write the (std)output of your program into the file foo.txt:

以下将程序的(std)输出写入文件foo.txt:

./myprogram > foo.txt

if you also want to see the output, pipe it through tee (which will write it's stdin to stdout and a file)

如果您还想查看输出,请将其通过tee(将其stdin写入stdout和文件)进行管道传输

./myprogram | tee foo.txt

if you are absolutely sure that you want to do all this from within your program, you might want to write your own write function, which writes to both stdout and a file.

如果您完全确定要在程序中执行所有这些操作,则可能需要编写自己的写入函数,该函数会写入stdout和文件。

ssize_t mywrite(int fd, const void *buf, size_t count) {
    write(fd, buf, count);
    return write(STDOUT_FILENO, buf, count);
}

and then use it, instead of write (note that you should pass the filedescriptor of an opened file tomywrite; it will also write to STDOUT)

然后使用它,而不是写(注意你应该传递打开的文件tomywrite的filedescriptor;它也会写入STDOUT)

#2


0  

It's not clear why you don't simply open() a new path and write() to that instead of writing to stdout. If that's not an option (it appears to be an option, the best one, in your example code), you can perform run-time redirection using this pseudo code.

目前尚不清楚为什么你不打开()一个新的路径并写()而不是写入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);

#3


0  

just open a file and use its file descriptor:

只需打开一个文件并使用其文件描述符:

#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); 
}

#1


1  

why not do that outside of your program?

为什么不在你的程序之外呢?

the following will write the (std)output of your program into the file foo.txt:

以下将程序的(std)输出写入文件foo.txt:

./myprogram > foo.txt

if you also want to see the output, pipe it through tee (which will write it's stdin to stdout and a file)

如果您还想查看输出,请将其通过tee(将其stdin写入stdout和文件)进行管道传输

./myprogram | tee foo.txt

if you are absolutely sure that you want to do all this from within your program, you might want to write your own write function, which writes to both stdout and a file.

如果您完全确定要在程序中执行所有这些操作,则可能需要编写自己的写入函数,该函数会写入stdout和文件。

ssize_t mywrite(int fd, const void *buf, size_t count) {
    write(fd, buf, count);
    return write(STDOUT_FILENO, buf, count);
}

and then use it, instead of write (note that you should pass the filedescriptor of an opened file tomywrite; it will also write to STDOUT)

然后使用它,而不是写(注意你应该传递打开的文件tomywrite的filedescriptor;它也会写入STDOUT)

#2


0  

It's not clear why you don't simply open() a new path and write() to that instead of writing to stdout. If that's not an option (it appears to be an option, the best one, in your example code), you can perform run-time redirection using this pseudo code.

目前尚不清楚为什么你不打开()一个新的路径并写()而不是写入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);

#3


0  

just open a file and use its file descriptor:

只需打开一个文件并使用其文件描述符:

#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); 
}

相关文章