I have this code:


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

#define MAX_LIN 80
#define SIGN '-'

void Fatal_Errorl(int num_error, char *string)
{
   switch(num_error)
    {
       case 1:
                fprintf(stderr, "Sintax:\n\nhead [-n] [File]\n\n");
                break;
        case 2:  fprintf(stderr,"Imp. to open file '%s'\n", string);
               break;
    }
 exit(num_error);
}

main(int argc, char *argv[])
{
   FILE *fp = stdin;
   char s[MAX_LIN+1];
   int i=0;
   int n_lins = 10;
   
   
switch(argc)
 {
   case 1: break;
   case 2: if (argv[1][0] ==SIGN)
             n_lins = atoi(argv[1]+1);
           else
             if (!(fp=fopen(argv[1],"r")))
               Fatal_Error(2, argv[1]);
             break;
   case 3: if (argv[1][0]!=SIGN)
               Fatal_Error(2,argv[2]);
           else
           { n_lins = atoi(argv[1]+1);
             if (!(fp=fopen(argv[2],"r")))
                Fatal_Error(2,argv[2]);
           }
           break;
           
           
  default: Fatal_Error(1,"");
 }
 
 while (fgets(s,MAX_LIN+1,fp)!=NULL && i++<n_lins)
  printf(s);
 fclose(fp);
}



Imagine the program is called head.

If I write this in the command prompt:

"head | dir"

It displays dir but instead of showing the path at the bottom and the blinking _, it lets you write whatever you want and only goes back to normal if you press Ctr+Z (EOF) or Ctr+C.

So, from what I know about pipes, the command does this:

head > tmp file
dir < tmp file
delete tmp

So, head's output is send to the tmp file... only what is head's output?
Then that output is send to dir as input.

So, what is exactly being sent as input to dir?
Not really sure if you are speaking Spanish now, but I am concerned about this line of code:


Code:
main(int argc, char *argv[])


main needs a return type, and should usually be int.

I'll take a look and see what I can find.
MateoConLechuga wrote:
Not really sure if you are speaking Spanish now, but I am concerned about this line of code:


Code:
main(int argc, char *argv[])


main needs a return type, and should usually be int.

I'll take a look and see what I can find.


Oops I forgot to translate. It's portuguese. There, I fixed it.

And thanks
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement