I have need to find a particular string from a text file. The user inputs the string they're looking for and the program searches through the opened text file to find that string. Is that possible?

void exam()
{
    char name[50], rollno[50];
    FILE *search;

    printf("\t\t________________________________");
    printf("\n\n\t\t\tPortal Examination");
    printf("\n\t\t_______________________________");
    printf("\n\tEnter Name : ");
    scanf("%s", name);
    search = fopen("Students.txt", "r");
    //not sure where to go from here

}

Dani AI

Generated

Short answer: yes. Two practical patterns work well depending on file size and exact needs — a streaming, line-by-line scan (low memory, immediate results) or reading the whole file into memory (simpler, faster for repeated searches). and already pointed those directions; below is a compact, safe example for the streaming approach and a few important tips and tradeoffs.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void search_file_lines(const char *filename, const char *needle) {
    FILE *f = fopen(filename, "r");
    if (!f) { perror("fopen"); return; }

    char line[1024];
    size_t lineno = 0;
    while (fgets(line, sizeof line, f)) {
        lineno++;
        char *p = line;
        while ((p = strstr(p, needle)) != NULL) {
            size_t col = (size_t)(p - line) + 1;
            printf("Found: line %zu, col %zu: %s", lineno, col, line);
            p++; /* continue after this match */
        }
    }
    fclose(f);
}

Notes and troubleshooting:

  • fgets truncates lines longer than the buffer; use a larger buffer, GNU getline, or read chunks and join if very long lines are expected.
  • For case-insensitive matching use strcasestr where available or make lowercase copies of both haystack and needle before searching.
  • Whole-file approach: fseek/ftell to get size, malloc(size+1), fread, null-terminate, then strstr. That is simpler for multiple searches but can blow memory on very large files — consider mmap or external tools for multi-GB files.
  • Read the search string with fgets (trim newline) so multi-word terms work; avoid unbounded scanf.
  • Always check return values (fopen, fread, malloc) and free resources.

This gives a straightforward, safe starting point you can adapt for highlighting context, counting matches, or reporting byte offsets.

Recommended Answers

All 2 Replies

To answer your question - yes it is possible.

I am not a C expert, but the principle I would use is to read in line by line. For each line see if the users' string exists. This assumes that the string you are searching for is completely on a line.

Assuming that the text file will never be a super huge file, you can read the whole file into memory and do multiple searches without reading the file each time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.