How to read a text file line by line and print only the lines that include a specific word in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

int main() {
    FILE* fp = fopen("d:\\data.txt", "r");
    char line[80];

    if (!fp) {
        puts("File doesn't exist");
        return 1;
    }

    // char *strstr (const char *str1, const char *str2);
    // returns a pointer to the first characters of str2 in str1 or null pointer if not found

    while (fgets(line, sizeof(line), fp)) {
        if (strstr(line, "c++")) {
            printf("%s", line);
        }
    }

    fclose(fp);

    return 0;
}




/*
run:

C C++ C# Java
C++
C++ is a high-level
Is C++ harder than Python
Visual Studio C/C++ IDE
Learn C++ 
C++ Programming

*/


 



answered Oct 25, 2024 by avibootz
edited Oct 31, 2024 by avibootz

Related questions

1 answer 165 views
1 answer 167 views
167 views asked Apr 8, 2021 by avibootz
2 answers 250 views
1 answer 106 views
...