简体   繁体   中英

C Unix program without warnings still not working

I would like to know if somebody could help me with this code(C Unix), because I solved all the warnings but my program is still not working...it doesn't create the .txt neither asking me to introduce text...Is everything in the correct order/place? Whats wrong? I really need your help, THANKS A LOT. Sorry because it is in Spanish.

#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("%s\n", bufferLectura);
    }
}

void ProcesoHijo2() { 
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);

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

    while(1) { 

        fgets(bufferLectura,255,fp); 
        descrFichero = open("salida.txt",O_APPEND|O_WRONLY, 0600);
        write(descrFichero, bufferLectura, strlen(bufferLectura));

    }



}

void ProcesoPadre() { 
char bufferLectura[256];

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

    printf("[Proceso padre]Introduce un texto, o exit para salir");//[Parent process]insert text or exit
    fgets(bufferLectura, 255,stdin);

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

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


}

You open the file three separate times (in three separate modes) over the course of 6 lines of code (not counting blank lines) in ProcesoHijo2 :

descrFichero = open("salida.txt", O_CREAT|O_TRUNC, 0600);  /* One */
descrFichero = open("salida.txt", O_RDWR | O_TRUNC, 0600); /* Two */

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

while(1) { 

    fgets(bufferLectura,255,fp); 
    descrFichero = open("salida.txt",O_APPEND|O_WRONLY, 0600); /* Three */
    write(descrFichero, bufferLectura, strlen(bufferLectura));
}

The first truncates it, the second truncates it. The third time opens the file over and over again (each time through the while loop), even though you've already opened it twice previously using the same file descriptor descrFichero .

Replace the first one with something like this, and delete the other two calls to open :

descrFichero = open("salida.txt", O_APPEND | O_TRUNC | O_RDWR, 0600);

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