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 430227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:51:16+00:00 2026-05-12T19:51:16+00:00

The following code reads a text file one character at the time and print

  • 0

The following code reads a text file one character at the time and print it to stdout:

#include <stdio.h>

int main()
{
    char file_to_open[] = "text_file.txt", ch;
    FILE *file_ptr;

    if((file_ptr = fopen(file_to_open, "r")) != NULL)
    {
        while((ch = fgetc(file_ptr)) != EOF)
        {
            putchar(ch);
        }
    }
    else
    {
        printf("Could not open %s\n", file_to_open);
        return 1;
    }
    return(0);
}

But instead of printing to stdout [putchar(ch)] I want to search the file for specific strings provided in another textfile ie. strings.txt and output the line with the match to out.txt

text_file.txt:

1993 - 1999 Pentium
1997 - 1999 Pentium II
1999 - 2003 Pentium III
1998 - 2009 Xeon
2006 - 2009 Intel Core 2

strings.txt:

Nehalem
AMD Athlon
Pentium

In this case the three first lines of text_file.txt would match. I have done some research on file operations in C, and it seems that I can read one character at the time with fgetc [like I do in my code], one line with fgets and one block with fread, but no word as I guess would be perfect in my situation?

  • 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-05-12T19:51:16+00:00Added an answer on May 12, 2026 at 7:51 pm

    I am assuming this is a learning exercise and you are simply looking for a place to start. Otherwise, you should not reinvent the wheel.

    The code below should give you an idea of what is involved. It is a program that allows you to specify the name of file to be searched and a single argument to search in that file. You should be able to modify this to put the phrases to search for in an array of strings and check if any of the words in that array appear in any of the lines read.

    The key function you are looking for is strstr.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #ifdef DEBUG
    #define INITIAL_ALLOC 2
    #else
    #define INITIAL_ALLOC 512
    #endif
    
    char *
    read_line(FILE *fin) {
        char *buffer;
        char *tmp;
        int read_chars = 0;
        int bufsize = INITIAL_ALLOC;
        char *line = malloc(bufsize);
    
        if ( !line ) {
            return NULL;
        }
    
        buffer = line;
    
        while ( fgets(buffer, bufsize - read_chars, fin) ) {
            read_chars = strlen(line);
    
            if ( line[read_chars - 1] == '\n' ) {
                line[read_chars - 1] = '\0';
                return line;
            }
    
            else {
                bufsize = 2 * bufsize;
                tmp = realloc(line, bufsize);
                if ( tmp ) {
                    line = tmp;
                    buffer = line + read_chars;
                }
                else {
                    free(line);
                    return NULL;
                }
            }
        }
        return NULL;
    }
    
    int
    main(int argc, char *argv[]) {
        FILE *fin;
        char *line;
    
        if ( argc != 3 ) {
            return EXIT_FAILURE;
        }
    
        fin = fopen(argv[1], "r");
    
        if ( fin ) {
            while ( line = read_line(fin) ) {
                if ( strstr(line, argv[2]) ){
                    fprintf(stdout, "%s\n", line);
                }
                free(line);
            }
        }
    
        fclose(fin);
        return 0;
    }
    

    Sample output:

    E:\Temp> searcher.exe searcher.c char
    char *
        char *buffer;
        char *tmp;
        int read_chars = 0;
        char *line = malloc(bufsize);
        while ( fgets(buffer, bufsize - read_chars, fin) ) {
            read_chars = strlen(line);
            if ( line[read_chars - 1] == '\n' ) {
                line[read_chars - 1] = '\0';
                    buffer = line + read_chars;
    main(int argc, char *argv[]) {
        char *line;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which reads in the follow file, append a \r\n
So I have a text file that reads as the following -9 5.23 b
I'm using the following code.. $(document).ready(function(){ $(#test a).click(function(){ var labelTo = $(this).text(); window.location =
I have the following code that reads the content of a url public static
I have the following code in a few activities and it reads and parses
I'm following a book on c, and I come to some code that reads
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have application reads in data from text file. Recently I realized that I
I have the following javascript into a index.php file: <script type=text/javascript> $('.showpics, .showpics2').live('click', function()
i created a text file list.txt in commonapplicationdatafolder by using the following VBscript.i am

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.