|
Home > Archive > Unix Programming > September 2007 > using fork and pipe
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
using fork and pipe
|
|
| Bhavin 2007-09-28, 1:24 pm |
| i need to implement a program which will squeeze all the redundant
white space , new line character, and tab, program is just read one
file perform above task and display it.
That thing i already did, problem is that i need to create one child
process which will read one character at a time and it will return the
result to parent process using pipe to display it(i.e parent will
print the character ) and repeat the process.
I already coded it and getting some error please help me to solve the
error....... Thank you
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int MAX = 100;
int main(int argc, char *argv[])
{
FILE *fopen(), *fp;
char c[MAX];
char strr[MAX];
int count1;
int pipe_fd[2];
if (pipe(pipe_fd) < 0)
{
perror("Pipe call");
return(1);
}
if( argc > 2 )
printf(" Too Many arguement. \n");
fp = fopen( argv[1], "r" );
if ( fp == NULL )
{
printf("Cannot open %s for reading \n", argv[1] );
exit(0);
}
count1 = 1 ;
c = getc( fp ) ;
while ( c != EOF )
{
if ( c == ' ' )
{
putchar(c);
count1=count1+1;
if ( count1 == 26 )
{
count1 = 1;
printf("\n");
}
c=getc(fp);
while(c == ' ')
{
c=getc(fp);
}
}
if ( c == '\n' || c == '\t' )
{
while( c == '\n' || c == '\t' )
{
c=getc( fp );
}
}
if ( c != '\n' || c != '\t' || c != ' ' )
{
// putchar( c ) ; /* Display character */
write(pipe_fd[1], c, strlen(c));
close(pipe_fd[1]);
count1 = count1+ 1 ;
c = getc ( fp );
if ( count1 == 26 )
{
count1 = 1;
printf("\n");
}
}
read(pipe_fd[0], strr, 1);
printf("%s", strr);
close(pipe_fd[0]);
}
fclose( fp );
}
| |
| Daniel C. Bastos 2007-09-28, 7:21 pm |
| Bhavin <v2desperado@gmail.com> writes:
> i need to implement a program which will squeeze all the redundant
> white space , new line character, and tab, program is just read one
> file perform above task and display it.
Okay.
> That thing i already did, problem is that i need to create one child
> process which will read one character at a time and it will return the
> result to parent process using pipe to display it(i.e parent will
> print the character ) and repeat the process.
Read a line, operate on it, and repeat. Why all the complication above?
| |
| Bhavin 2007-09-28, 7:21 pm |
| On Sep 27, 5:37 pm, dbas...@yahoo.com.br (Daniel C. Bastos) wrote:
> Bhavin <v2desper...@gmail.com> writes:
>
> Okay.
>
>
> Read a line, operate on it, and repeat. Why all the complication above?
because that is what i have to do, i know i can use read line but they
want it character wise, please if you can help me, I really
appreciatted your effort
| |
| Scott Lurndal 2007-09-28, 7:21 pm |
| Bhavin <v2desperado@gmail.com> writes:
>On Sep 27, 5:37 pm, dbas...@yahoo.com.br (Daniel C. Bastos) wrote:
>
>because that is what i have to do, i know i can use read line but they
>want it character wise, please if you can help me, I really
>appreciatted your effort
>
Please ask your TA or professor to help you first.
scott
| |
| Barry Margolin 2007-09-29, 1:47 am |
| In article <1191001526.973942.324910@50g2000hsm.googlegroups.com>,
Bhavin <v2desperado@gmail.com> wrote:
> i need to implement a program which will squeeze all the redundant
> white space , new line character, and tab, program is just read one
> file perform above task and display it.
> That thing i already did, problem is that i need to create one child
> process which will read one character at a time and it will return the
> result to parent process using pipe to display it(i.e parent will
> print the character ) and repeat the process.
>
> I already coded it and getting some error please help me to solve the
> error....... Thank you
What is "some error"?
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
|
|
|
|
|