c - How to redirect standard output to a file — what's wrong with this code? -


i'm using c program on raspberry pi2 433mhz receiver read codes transmitted. program sniffing 433mhz codes.

to run it, use following command: sudo ./rfsniffer , if code found, program displays in console :

received 5204

but, able these codes in file, tried this:

sudo ./rfsniffer >> codes.txt

but nothing appended codes.txt file...and don't know why. what's wrong code? file empty.

here code :

#include "rcswitch.h" #include <stdlib.h> #include <stdio.h>  rcswitch myswitch;  int main(int argc, char *argv[]) {       int pin = 2;       if(wiringpisetup() == -1)        return 0;       myswitch = rcswitch();      myswitch.enablereceive(pin);       while(1) {        if (myswitch.available()) {          int value = myswitch.getreceivedvalue();          if (value == 0) {           printf("unknown encoding");         } else {                printf("received %i\n", myswitch.getreceivedvalue() );         }          myswitch.resetavailable();        }    }    exit(0);  } 

could problem exit(0) or printf() instead of else?

edit:

the program compiled wiringpi lib there flag '-lwiringpi'

the tool available here: https://github.com/ninjablocks/433utils/tree/master/rpi_utils

edit2:

i changed code to:

int main(int argc, char *argv[]) {    printf("yeah\n");   exit(0);  } 

and works with:

sudo sh -c './rfsniffer >> /home/pi/433utils/rpi_utils/codes.txt' 

so problem maybe while(1) { printf... }? or file written when exit(0) called?

you writing on stdout buffered default, , cannot see break, return or exit in loop, assume quit program ctrl-c.

and values buffered until there discarded. should fflush stdout after each write make sure received end in file :

 while(1) {   if (myswitch.available()) {     int value = myswitch.getreceivedvalue();     if (value == 0) {       printf("unknown encoding");     } else {           printf("received %i\n", myswitch.getreceivedvalue() );     }     fflush(stdout);   // force immediate output     myswitch.resetavailable();   } 

and anyway, having ctrl-c exit program not nice ...


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -