Sponsored Content
Full Discussion: Pipe error
Top Forums Programming Pipe error Post 302473838 by beandj on Monday 22nd of November 2010 02:30:54 PM
Old 11-22-2010
Pipe error

hi guys, o have a big error
in this program but i cant solve

someone ?!

Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

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

 int cont = 2, posicao; 
 char geraArquivo[100]= "|cat>>", espaco[3]=" ";
 char nomeArquivo[50], comando[1000], leitor[1000];

 if(argc>1)
 {
  strcpy(comando, argv[1]);
  while(cont< argc)
  {
   strcat(comando, espaco);
   strcat(comando, argv[cont]);
   cont++;
  }//fim do while

 printf("\nDigite o nome do arquivo: ");
 scanf("%s",nomeArquivo);
 printf("\n");
 strcat(geraArquivo,nomeArquivo);
 strcat(comando,geraArquivo);
 system(comando);
 }//fim do if

 else{
  printf("\nErro!\nDados para geração do arquivo não foram encontrados!\nPor favor entre com um comando do Sistema!\n");
  return 0;
 }//fim do else
 
 FILE *arq, *arq1, *arq2;
 arq = fopen(nomeArquivo,"r");
   arq1 = fopen("arquivoPai.txt","w+");
  arq2 = fopen("arquivoFilho.txt","w+");
 
   posicao = ftell(arq);


 int fp[2];
 int pf[2];
 int rc;
 int nbytes;
 char reader[1000];
 pipe(fp);
 pipe(pf);
 pid_t pid;
 pid = fork();

   while(!feof(arq))  //varrer arquivo
 {
  printf("OLHAE");
     if(pid == 0 && (posicao%2)==0)
  {
   close(fp[0]); // filho mandando pro pai fechar o "input"
   close(pf[1]);
   nbytes = read(pf[0], reader, sizeof(reader));
   fprintf(arq1,"%s \n",reader);
   fscanf(arq,"%s",leitor);
   write(fp[1],leitor, (strlen(leitor)+1));

  }
 
     else
  {
   close(fp[1]); // recebendo dados do filho fecharo "output"
   close(pf[0]);
   nbytes = read(fp[0],reader, sizeof(reader));
   fprintf(arq2,"%s \n",reader);
   fscanf(arq,"%s",leitor);
   write(pf[1],leitor,(strlen(leitor)+1));

  }

  posicao = ftell(arq);
 }
 
 exit(0);
 fclose(arq);
 fclose(arq1);
 fclose(arq2);
 

return 0;
}//fim da main

 

10 More Discussions You Might Find Interesting

1. AIX

broken pipe error

Hi, I am working on AIX 5.3 . I have client-server program which is in ProC.while sending packet to server i am getting error as broken pipe and program exiting. please help?/? (1 Reply)
Discussion started by: ajaysahoo
1 Replies

2. Programming

gzip on pipe error handling

Hi all... I have the following code: FILE *fp = popen(" gzip -dc /somemount/somefile.gz", "r"); while(fgets(buffer, 1024, fp)) { some code.... } "/somemount" is a mount of some network drive. Sometimes error occurs in while loop - I can see the following "Input/Output error"... (4 Replies)
Discussion started by: adm1n
4 Replies

3. Programming

Broken Pipe error

All, I am using the below code The C code : if ((fp2=fopen(szout_fname,"r"))==NULL) { sprintf(stream_ptr1,"cat %s | sort -t, -rn -k 11,11 | awk -F\",\" '{ \ if ( \$3 ==\"%s\" ) {print... (0 Replies)
Discussion started by: arunkumar_mca
0 Replies

4. UNIX for Dummies Questions & Answers

echo: write error: Broken pipe ??

I want to try the unix pipe, the command is like this: echo new | find . the standard output of the echo should be "new", then I guess find command will use this output as input to find the file named "new". But the output is all the file names in my current dir, the last line is "echo: write... (5 Replies)
Discussion started by: andrewust
5 Replies

5. Shell Programming and Scripting

Broken Pipe error

Hello while doing sftp over server "A" , i am getting a broken pipe error i.e cat: write error: Broken pipe what does that mean? please let me know if you want any other info on this.. (3 Replies)
Discussion started by: urfrnddpk
3 Replies

6. UNIX for Dummies Questions & Answers

broken pipe error

I'm new to scripting, and this forum has been invaluable in helping me out. I'm hoping I can get some personal help now though. I have a korn script that takes a list of servers and either telnets or sshs into it (only some are set up for ssh). What I'm doing now is trying to telnet first, and... (10 Replies)
Discussion started by: aimeet
10 Replies

7. Shell Programming and Scripting

Pipe causing last command error to not function

Hi I am quite new to scripting and cannot work out how to do the following - I want to pipe to a log file and then use the "last statement error" in an if statement after, and this doesn't work because it checks the pipe statement instead of the script. Example: executteTheScript $var |... (4 Replies)
Discussion started by: erjorgito
4 Replies

8. Shell Programming and Scripting

Capture error before pipe

Hi, I have a script that runs a tar command to standard out then pipes to a gzip: tar cfE - * | gzip -c > OUT.gz At the moment, even if the tar fails (e.g. because of lack of disk space), the gzip still runs successfully. Is there a way to make the whole line exit with a non-zero error... (6 Replies)
Discussion started by: Catullus
6 Replies

9. Shell Programming and Scripting

Pipe, permission denied error

Hello, I am trying to stream lines written into file. When first source is down, I expect the code to swap to second line and run it. Script below works as usual and it prints out each line of the input file (s.txt): #!/bin/bash while read -r line; do echo "$line" done <"$1" exit 0Output... (12 Replies)
Discussion started by: baris35
12 Replies

10. Programming

Pipe usage error while visiting directories

To begin with FYI, I really struggled with the question before asking to simplify as much as I can around 1 hour and to increase the code's readability I neglect error checks. I intend to communicate parent and child using PIPE. In the following program I do traverse given path and its... (2 Replies)
Discussion started by: beginnerboy
2 Replies
All times are GMT -4. The time now is 02:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy