nnj20229@uxa.cso.uiuc.edu (Nesha Nicole Jones) (05/31/91)
I wrote a program that acts like a grep only it greps the line you are looking for and the line above it. I would like to be able to do something like: rest "restart" /usr/bin/adm/sa/sar24 or rest "restart" < /usr/bin/adm/sa/sar24 currently my program does the second of the two listed above. If I use file pointers and fopen will the program still work if I try to pipe input into it? below are the two functions I currently use to get the line and check to see if the string is in the line. int getline(char s[], int lim) /* reads in the line from the file/stdin */ { int c,i; i = 0; while (--lim > 0 && (c=getchar()) != EOF && c != '\n') s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return(i); } /* if the following function returns a value greater than 0 the it writes the line that was rad in getline to to the screen */ int index(char s[], char t[]) { int i,j,k; for (i = 0; s[i] != '\0'; i++) { for (j = i, k = 0; t[k] !='\0' && s[j] == t[k]; j++,k++) ; if (t[k] == '\0') return(i); } return(-1); }
rjohnson@shell.com (Roy Johnson) (06/04/91)
In article <1991May30.191116.19256@ux1.cso.uiuc.edu> nnj20229@uxa.cso.uiuc.edu (Nesha Nicole Jones) writes: > > I wrote a program that acts like a grep only it greps the line you are > looking for and the line above it. I would like to be able to do > something like: > rest "restart" /usr/bin/adm/sa/sar24 > or > rest "restart" < /usr/bin/adm/sa/sar24 > > > currently my program does the second of the two listed above. If I use > file pointers and fopen will the program still work if I try to pipe input > into it? FILE *freopen(filename, type, stream) char *filename, *type; FILE *stream; freopen() opens the file named by filename and associates the stream pointed to by stream with it. The type argument is used just as in fopen. The original stream is closed, regardless of whether the open ultimately succeeds. If the open succeeds, freopen() returns the original value of stream. freopen() is typically used to attach the preopened streams associated with stdin, stdout, and stderr to other files. So if ((argc == 3) && (freopen(argv[2], "r", stdin) != stdin)) { perror(argv[2]); exit(1); } in your command line parsing phase should make the file opening transparent to your program (no messy FILE pointers). -- =============== !You!can't!get!here!from!there!rjohnson =============== Feel free to correct me, but don't preface your correction with "BZZT!" Roy Johnson, Shell Development Company