Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:43:56+00:00 2026-06-03T22:43:56+00:00

In a C program, I need to search for an exact string in a

  • 0

In a C program, I need to search for an exact string in a normal file (I use Linux). How shall I do in order to search?

My first assumption consisted of moving each line of the file to the RAM (via fgets()) and, after each move, check if that line was the right string. If it isn’t, a loop will re-call fgets() and check the strings until EOF.

But what happens with a file with 150 million lines? Happens that this kind of sequential search seems to be ineffective at all.

However, I was thinking about a kind of binary search, using the insertion sort in order to sort the lines my program add to the file (it adds one line around every 3 seconds, immediately after have checked that that line doesn’t appear in the strings file). But then I gave up because I first would needed to move the lines to the RAM, using the same time I would have used for a sequential search. Thus I chose the sequential search.

Is this assumption right? Or is there a better way? I really hope so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:43:57+00:00Added an answer on June 3, 2026 at 10:43 pm

    You could use mmap to map the entire file into memory, and then do a strnstr search:

    #include <sys/mman.h>
    
    const char *fileName = "~/test.txt";
    long fileLength;
    
    // open file and get it's length
    FILE *fptr = fopen(fileName, "r");
    
    if (fptr == NULL || ferror(fptr))
    {
        perror("could not open file");
        fclose(fptr);
        return 0;
    }
    
    fseek(fptr, 0, SEEK_END);
    fileLength = ftell(fptr);
    // return to the start of the file
    fseek(fptr, 0, SEEK_SET);
    
    // map the file
    char *fileData = mmap(NULL, fileLength, PROT_READ, MAP_FILE | MAP_SHARED, fileno(fptr), 0);
    
    if (fileData == MAP_FAILED)
        perror("could not map file");
    
    // scan the file
    char stringToSearchFor[] = "this is my string!";
    if (strnstr(fileData, stringToSearchFor, fileLength) != NULL)
    {
        printf("file contains the string!");
    }
    else {
        printf("file doesn't contain the string");
    }
    
    // clean up our code
    munmap(fileData, fileLength);
    fclose(fptr); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help for creating a File and String search engine. The program needs
I have a program that I need to be able to search a file
I need to use a string query to make a DB search for a
In my program I need to search in a quite big string (~1 mb)
I need to write an AS3 program to search for a certain keyword in
How can I get Google search results from inside a program? I need to
For a part of a program i need the following 2 methods. The first
I need my program to create and edit a config file, which would contain
I'm creating a program where I need to search an FTP server and download
I need to search a word exact same character or similar character not in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.