Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/08/2015, 13:54
xD_avid
 
Fecha de Ingreso: enero-2011
Mensajes: 13
Antigüedad: 13 años, 3 meses
Puntos: 0
Pregunta Escribir en fichero

Buenas, hace mucho que no programo en C y he vuelto a las andadas. Estoy intentando insertar una cadena de texto en un fichero de texto, pero parece ser que la codificación no la hace bien, no recuerdo como se trabajaba con estas cosas, por lo que no encuentro el fallo por mucho que busque en internet. Os pongo el código y el resultado.

Código:
#include "RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>     
     
RCSwitch mySwitch;

using namespace std; 


int main(int argc, char *argv[]) {

        // Lineas propias
        FILE *fp;
        fp=fopen("/home/pi/historial.txt", "r+b");
        string accion;
  
     // This pin is not the first pin on the RPi GPIO header!
     // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     // for more information.
     int PIN = 2;
     
     if(wiringPiSetup() == -1)
       return 0;

     mySwitch = RCSwitch();
     mySwitch.enableReceive(PIN);  // Receiver on inerrupt 0 => that is pin #2
     
    
     while(1) {
  
      if (mySwitch.available()) {
    
        int value = mySwitch.getReceivedValue();
        if (value == 0) {
          printf("Unknown encoding\n");
        } else {    
   
          printf("Received %i\n", mySwitch.getReceivedValue() );

                if (value == 3131){
                        accion = "Encender luz dormitorio";}
        }

        fseek (fp, 0, SEEK_END);
        fwrite(&accion, sizeof(string), accion.length(), fp);
    
        mySwitch.resetAvailable();
    
      }
      
  
  }
  fclose(fp);
  exit(0);     


}
Simplemente debería añadir al final del fichero Encender luz dormitorio, pero en su lugar me muestra este resultado:
Código:
R-ÿÿÿÿÿÿÿÿ^
KÚ¶;
   P-èȶ0Ú¶äø©¾¨¥øŠR-ÿÿÿÿÿÿÿÿ^
KÚ¶;
   P-èȶ0Ú¶äø©¾¨¥øŠpi@raspberrypi:~$