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.
/*
* C Program to List All Lines containing a given String
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int search(FILE *, char *);
void main(int argc, char * argv[])
{
FILE *fp1;
int p;
fp1 = fopen(argv[1], "r+");
if (fp1 == NULL)
{
printf("cannot open the file ");
exit(0);
}
search(fp1, argv[2]);
fclose(fp1);
}
/* Searches the lines */
int search(FILE *fp, char * str)
{
FILE *fp1;
fp1 = fopen("fp1","w");
char s[10],c;
int len = strlen(str);
int i = 0;
int d;
int seek = fseek(fp, 0, 0);
c = fgetc(fp);
while (c != EOF)
{
if (c == ' ' || c == '\n')
{
s[i] = '\0';
i = 0;
if (strcmp(s, str) == 0)
{
while (c = fgetc(fp) != '\n')
{
fseek(fp, -2L, 1);
d = ftell(fp);
}
while ((c = fgetc(fp)) != '\n')
{
fputc(c, fp1);
}
}
}
else
{
s[i] = c;
i++;
}
c = fgetc(fp);
}
return 1;
}
$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.
Related Posts:
- Practice BCA MCQs
- Apply for C Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Check C Books