i need to find a way to print the number of arguments --- example ----

./a02 er t r

child process : counts four argument ----

what the output is now ---- er ; t;r;

and problem 2)

how can i make a call to reader for every character----instead of how i am doing it now---
each character should be a call to reader...

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



void writer (const char* message, int count, FILE* stream)
{
    for (; count > 0; --count) {
      
       fprintf (stream, "%s\n", message);
       fflush (stream);
     
       sleep (1);
    }
}



void reader (FILE* stream)
{
    int temp,temp1;

       
    char buffer[1024];
  
      
    while (!feof (stream)
           && !ferror (stream)
           && fgets (buffer,sizeof (buffer), stream) != NULL)
      {
while(buffer<0)
{

temp++;

}
    // printf("%d\n",temp);    
     fputs (buffer,stdout);
    } 
}

int main (int argc,char **argv)
{
    int fds[2];
    pid_t pid;
    int i =0;
   
       
    pipe (fds);
  
    pid = fork ();
    if (pid == (pid_t) 0) {
      FILE* stream;
    

      close (fds[1]);
    

      stream = fdopen (fds[0], "r");
      reader (stream);
                             
        close (fds[0]);
      }
      else {
        
        FILE* stream;
       
        close (fds[0]);
       
         
        stream = fdopen (fds[1], "w");
      while(i < (argc-1)){
        i++;
         
        writer (argv[i], 1, stream);}
        close (fds[1]);
      }

      return 0;
}

line 33: buffer is a character array, therefore it will never be < 0 and that while loop makes absolutely no sense.

yeah, i forgot to take it out i was trying stuff.... but anyway ,,, how would i display the number arguments ,, not the actual arguments---

./a02 23 33

needed output ---------there are 4 characters------

argc contains the number of command-line arguments. in your example it will be 3, the filename + 2 arguments = 3. If you want the number of characters in the arguments just run through argv and count them.

int main(int argc, char* argv[])
{
    int counter = 0;
    int i;
    for(i = 0; i < argc; ++i)
        counter += strlen(argv[0]);
   printf("The number of characters is %d\n", counter);
   return 0;
}

argc contains the number of command-line arguments. in your example it will be 3, the filename + 2 arguments = 3. If you want the number of characters in the arguments just run through argv and count them.

int main(int argc, char* argv[])
{
    int counter = 0;
    int i;
    for(i = 0; i < argc; ++i)
        counter += strlen(argv[0]);
   printf("The number of characters is %d\n", counter);
   return 0;
}

this does not work, what about white spaces.... ./a02 23 3

says the count is one--- needs to be three ---

>>this does not work, what about white spaces

The command interpret ignores all white space (except to separate the actual parameters), so programs don't see them. These are all the same to your program, they both produce the same results.

./a02 23 3
./a02     23          3

>>says the count is one--- needs to be three
The value of argc should be 3 in the example you posted. You probably did not test the program correctly.

>>this does not work, what about white spaces

The command interpret ignores all white space (except to separate the actual parameters), so programs don't see them. These are all the same to your program, they both produce the same results.

./a02 23 3
./a02     23          3

>>says the count is one--- needs to be three
The value of argc should be 3 in the example you posted. You probably did not test the program correctly.

you were right --- thanks , but how do i send characters one at a time through the pipe, using one call to writer() for each character.

I would think that would be pretty obvious if you wrote all that code yourself. This will send the entire string to writer() -- you should lete writer() worry about how to send the characters through the pipe.

else {
        
        FILE* stream;
       
        close (fds[0]);
       
         
      stream = fdopen (fds[1], "w");
      for(i = 1; i < argc; i++)
     {
        writer (argv[i], 1, stream);}
      }
      close (fds[1]); // not sure about the placement of this line.

actually writer() coulld be simplified like this: (removing one parameter)

void writer (const char* message,  FILE* stream)
{
       fprintf (stream, "%s\n", message);
       fflush (stream);
}

But if the program on the receiving end needs all the arguments including spaces, then you can easily construct a single string to represent that

int main(int argc, char **argv)
{
    int i;
    char buffer[255] = {0};
    for(i = 1; i < argc; i++)
    {
         strcat(buffer, argv[i]);
         strcat(buffer, " ");
    }
    ...
    ...
    writer(buffer, stream);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.