C Program to List All Lines Containing a given String

This C Program listS all lines containing a given string.

Here is source code of the C Program to list all lines containing a given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to List All Lines containing a given String
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int search(FILE *, char *);
  9.  
  10. void main(int argc, char * argv[])
  11. {
  12.     FILE *fp1;
  13.     int p;
  14.  
  15.     fp1 = fopen(argv[1], "r+");
  16.     if (fp1 == NULL)
  17.     {
  18.         printf("cannot open the file ");
  19.         exit(0);
  20.     }
  21.     search(fp1, argv[2]);
  22.     fclose(fp1);
  23. }
  24.  
  25. /* Searches the lines */
  26. int search(FILE *fp, char * str)
  27. {
  28.     FILE *fp1;
  29.     fp1 = fopen("fp1","w");
  30.     char s[10],c;
  31.     int len = strlen(str);
  32.     int i = 0;
  33.     int d;
  34.     int seek = fseek(fp, 0, 0);
  35.     c = fgetc(fp);
  36.     while (c != EOF)
  37.     {
  38.         if (c == ' ' || c == '\n')
  39.         {
  40.             s[i] = '\0';
  41.             i = 0;
  42.             if (strcmp(s, str) == 0)
  43.             {
  44.                 while (c = fgetc(fp) != '\n')
  45.                 {
  46.                     fseek(fp, -2L, 1);
  47.                     d = ftell(fp);
  48.                 }
  49.                 while ((c = fgetc(fp)) != '\n')
  50.                 {
  51.                     fputc(c, fp1);
  52.                 }
  53.             }
  54.         }
  55.         else
  56.         {
  57.             s[i] = c;
  58.             i++;
  59.         }
  60.         c = fgetc(fp);
  61.     }
  62.     return 1;
  63. }

$cat example
hi hello everyone
again hi to the late comers
welcome to the class
 
$ cc file6.c
$ ./a.out example hi
hi hello everyone
again hi to the late comers

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.