[ create a new paste ] login | about

Link: http://codepad.org/dQt9ehKW    [ raw code | output | fork ]

C, pasted on Jul 3:
static char usage[] = "usgae: ./a.exe <word> <filename>\n";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 3 /* >= 2 */
char *getline(FILE *fp)
{
  static char inbuff[BUFFSIZE];
  char *outbuff_malloc, *tmpbuff;
  char *p, *q;
  int fEOL;
  if ((outbuff_malloc = malloc(1)) == NULL) {
    return NULL;
  }
  *outbuff_malloc = '\0';
  fEOL = 0;
  do {
    if ((q = fgets(inbuff, BUFFSIZE, fp)) == NULL)
      break;
    for (p = inbuff; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n') {
      *(p - 1) = '\0';
      fEOL = 1;
    }
    if ((tmpbuff = realloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) ==NULL) {
      free(outbuff_malloc);
      return NULL;
    }
    strcat(tmpbuff, inbuff);
    outbuff_malloc = tmpbuff;
  } while (!fEOL);
  if (q == NULL) {
    free(outbuff_malloc);
    return NULL;
  }
  return outbuff_malloc;
}
int main(int argc, char *argv[]) {
  char *p;
  FILE *fp;
  if (argc != 3) {
    fprintf(stderr,"%s", usage);
    return -1;
  }
  if ((fp = fopen(argv[2], "r")) == NULL) {
    fprintf(stderr, "cannot open the file %s.\n", argv[2]);
    return -1;
  }
  while ((p = getline(fp)) != NULL) {
    if (strstr(p, argv[1]) != NULL)
      printf("%s\n", p);
    free(p);
  }
  fclose(fp);
  return 0;
}
/* end */


Output:
1
2
3
usgae: ./a.exe <word> <filename>

Exited: ExitFailure 255


Create a new paste based on this one


Comments: