Resultados 1 al 2 de 2

Fallo en programa de C

  1. #1 Fallo en programa de C 
    Iniciado
    Fecha de ingreso
    Feb 2009
    Mensajes
    3
    Descargas
    0
    Uploads
    0
    Hola a todos. Ante todo daros las gracia spor este maravilloso foro, por el cual podemos todos aprender cada día más y más.

    Os comento mi problema. He estado haciendo un programa en C que no consigo hacerlo funcionar correctamente. El fallo es que al indicarle un valor de entrada, como por ejemplo "9", el programa me da un piso equivocado, el "2304". No consigo que me muestre bien el piso desde el que se tira el hijo. Lo he dado muchas vueltas pero no soy capaz de ver el fallo. Os agradeceria que me hecharais una mano. Muchas gracias:

    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>


    int main(void)
    {
    int fd;
    int floor;

    char buf[]="Legacy: Nothing I have, nothing I'll leave, nothing you'll have.\n\n";

    // We create a file...
    if ( (fd=creat("legacy.txt",0666))<0){
    perror("error creating the file\n\n");
    exit(1);
    }
    // ... and then we write the buffer on the file.
    printf("Parent: I'm going to write my legacy...\n\n");
    if ( write(fd,buf,64)!=64)
    perror("Parent: D'oh! Some error happened while writing!\n\n");
    else
    printf("Parent: I have successfully written my legacy!\n\n");

    int pid;
    pid = fork();

    if (pid < 0) { /* error occurred */
    perror("Parent: The fork failed!\n\n");
    exit(-1);
    }

    else if (pid==0) { /* child process */
    printf("Child: I am the child, and I'm going to read my father's legacy... Mwahahaha! \n\n");
    int n;
    char buf[1];
    fd=open("legacy.txt",O_RDONLY,0666);
    while ((n=read(fd,buf,1))>0)
    printf("%c",buf[0]);
    printf("\n\n");
    if (n<0) {
    perror("Read error occured\n\n");
    return -1;
    }
    }
    else { /* parent process */
    wait(&floor);
    printf("Parent: How do you dare to read my papers!\n\n" );
    }
    if (pid==0) { /* child process */
    printf("Child: How do you dare to leave me nothing! I cannot live since I know this...\n\n" );
    printf("Advertisement: Hey! From which floor should the child jump? Text 'JUMP' to 7717 followed by the floor number.\n\n");
    printf("Advertisement: Or you can just type it here:\n\n");
    scanf("%d", &floor);
    printf("\n\n");
    exit(floor);
    }
    close(fd);
    printf("Narrator: The whole family commited suicide.\n\n" );
    printf("Narrator: Oh, by the way, the son jumped from the %dth floor.\n\n", floor);
    return 0;

    }


    Muchas gracias.
    Citar  
     

  2. #2  
    Moderador HH
    Fecha de ingreso
    Sep 2003
    Mensajes
    1.384
    Descargas
    21
    Uploads
    5
    Creo que se usaban unas macros para obtener el valor del codigo que pasaste desde el hijo en floor.

    De todas maneras, lo podes hacer manualmente con floor>>8 para recuperar el codigo que guardaste y con floor & 0377 ves si el hijo termino bien o que paso.


    Saludos
    - Me desagrada
    - ¿Por qué?
    - No estoy a su altura.
    ¿Ha respondido así alguna vez un hombre?

    Friedrich Nietzsche



    Citar  
     

Temas similares

  1. Fallo al iniciar Vista, fallo disco duro.
    Por vinsanity en el foro WINDOWS
    Respuestas: 1
    Último mensaje: 16-12-2007, 22:57
  2. programa para programar o hacer programa!!!!??
    Por the_piyoyo en el foro GENERAL
    Respuestas: 1
    Último mensaje: 18-08-2006, 09:42
  3. Ejecutar un programa desde otro programa ( C++ )
    Por CAE en el foro PROGRAMACION DESKTOP
    Respuestas: 2
    Último mensaje: 19-01-2005, 00:14
  4. Fallo en los RW
    Por alcaide en el foro HARDWARE
    Respuestas: 3
    Último mensaje: 12-11-2003, 19:00
  5. Fallo en ASP
    Por DeBuG32 en el foro HACK HiSPANO
    Respuestas: 0
    Último mensaje: 21-05-2002, 17:38

Marcadores

Marcadores