简体   繁体   中英

Redirecting output to a file in linux

i am writing ac 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.

#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

why not do that outside of your program?

the following will write the (std)output of your program into the file 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)

./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.

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 to mywrite ; it will also write to STDOUT)

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM