简体   繁体   中英

fgets in this program

I'm trying to make a C program in UNIX in which the parent process generates two child processes. The parent will read data from stdin and will send it to his children. The 1st child will show on screen the text read from his father, to standard output. The 2nd child will show the read data in a file. When the parent has as input an "exit", he will send termination signals to his children and will exit.

So here is the complete code I did, but where I need help is in void ProcesoHijo2() . I still have a warning when I have to compile,this one:

74: warning: passing argument 3 of ‘fgets’ makes pointer from integer without a cast
/usr/include/stdio.h:624: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘int’ -->in 
void ProcesoHijo2()

The program is in spanish, so the variable names too, I hope that won't be a problem and you can help me soon because I'm desperate...THANK YOU!!!!

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>

int pidHijo1;
int descrTub1[2];

int pidHijo2;
int descrTub2[2];

void ProcesoHijo1();
void ProcesoHijo2();
void ProcesoPadre();

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


    pipe(descrTub1); 
    pipe(descrTub2);
        pidHijo1 = fork();

        if (pidHijo1 == 0) {
            ProcesoHijo1();
            return 0;
        }

    pidHijo2 = fork();

        if (pidHijo2 == 0)
            ProcesoHijo2();
        else
            ProcesoPadre();
}


void ProcesoHijo1() { 
char bufferLectura[256];

    close(0); 
    dup(descrTub1[0]); 

    while (1) { 
        fgets(bufferLectura, 255, stdin); 
        printf("%sn", bufferLectura);
    }
}

void ProcesoHijo2() { //-->So here is where I have trouble to program...I don't know if it's just something from inside this process or from above...
char bufferLectura[256];
int descrFichero;

    close(0); 
    dup(descrTub1[0]); 

    descrFichero = open("salida.txt", O_CREAT | O_TRUNC, 0600);
    descrFichero = open("salida.txt", O_RDWR | O_TRUNC, 0600);//-->do I need this one?

    while(1) { 
        fgets(bufferLectura,255,descrTub1[0]); //-->I have to read it from pipe and save it in bufferLectura, but I don't know how to put it...
        write(descrFichero, bufferLectura, strlen(bufferLectura));
        descrFichero = open("salida.txt", O_WRONLY|O_APPEND, 0600); //-->Is this correct?
    }
}

void ProcesoPadre() { 
char bufferLectura[256];

    close(2); 
    dup(descrTub1[1]); 

    printf("[Proceso padre]Introduce un texto, o exit para salir");
    fgets(bufferLectura, 255,stdin);

    while(strcmp(bufferLectura, "exitn")) { 
        fprintf(stderr,"%s/n", bufferLectura); 
        write(descrTub2[1], bufferLectura, strlen(bufferLectura)); 
        printf("[Proceso padre] Introduce un texto, o exit para salir ");
        fgets(bufferLectura, 255,stdin);
    }

    kill(pidHijo1, SIGTERM);
    kill(pidHijo2, SIGTERM);

}

pipe is OS/lower level and gives you a unix file descriptor ( int ). fgets is libstdc/higher level and uses FILE* . You can either read from the file low-level style (OS function read) or you can use fdopen to get a FILE* for your descrTub1[0] .

The function fgets wants a pointer to FILE , while you are giving it a file descriptor which is an int .

You can get a FILE pointer from a file descriptor by using the function fdopen :

FILE *fp = fdopen(descrTub1[0], "r");

And use that in the call to fgets :

fgets(bufferLectura,255,fp);

dup returns file descriptors which are int & not file streams ie FILE * so you are getting the warning. Use read instead of fgets or use fdopen to get file stream FILE* for the file descriptor to use with fgets . Open the file only once with either one of the open calls outside the while as per your need and close once you are done with the operations. As you are using a lot of system calls check the return value & in case of failure use perror or srterror to print meaningful error messages (which are useful to debug) maybe something on these lines:

if( dup(descrTub1[0]) < 0 )
{
    perror("dup");
    /*Handle error */
}

Hope this helps!

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