c - Closing a pipe causes the wrong line to be read from a file (that's independent of the pipe) -


i'm writing program has number of child processes. parent has 2 pipes write child, each pipe dup2ed separate file descriptor in child. parent reads through script file number of commands work needs do. seems work fine except i've noticed if try close pipe parent child if next line in script file blank read in weirdly. if print out comes out ���< program (understandably) doesn't know with.

there seemingly no link @ between file pointer i'm reading in , pipe i'm closing.

my code reading in lines in below. works normally, in case doesn't work , can't work out why.

char *get_script_line(file *script) {     char *line;     char charread;     int placeinstr = 0;     int currentsizeofstr = 80;     int maxstrlength = 64;      /* initialize line */     line = (char *)malloc(sizeof(char)*currentsizeofstr);      while ((charread = fgetc(script)) != eof && charread != '\n') {         /* read each char input , put in array */          line[placeinstr] = charread;         placeinstr++;         if (placeinstr == currentsizeofstr) {             /* array run out of room, need allocate */              /* more space */              currentsizeofstr = placeinstr + maxstrlength;             line = realloc(line, currentsizeofstr);             }     }      if (charread == eof && placeinstr == 0)     {         /* eof, return null */         return null;     }      return line; } 

and when close pipe i'm doing this:

fflush(pipe); fclose(pipe); 

does have idea of might doing wrong? or have sort of idea of how debug problem?

edit: clear input script might (basically) this:

start new child , set pipes etc send eof 1 of pipes child (blank line) else 

it works fine otherwise , i'm sure i'm closing pipes i'm doing send eof supposed to.

i suggest running program under valgrind check memory errors.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -