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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:50:41+00:00 2026-05-18T04:50:41+00:00

I have a program written in C which should count the number of times

  • 0

I have a program written in C which should count the number of times the word “the” appears in text files that are given as arguments.But the program keeps giving a segmentation fault error and I have no more ideas on how to solve this.Any help would be appreciated.Thank you!

Here is the code:


#include <stdio.h>
#include <string.h>
void main(int argc, char *argv[])
{ 
  int h,i;
  FILE *fp;
  char* mess;
  for(i=1; i < argc; i++)
  { 
     h=0;
     fp=fopen(argv[i],"r");
      while (!feof(fp))
      { 
        fscanf(fp,"%s",mess);
        if (strcmp(mess,"the")==0)
          h++;
      }
      printf("The file %s contains the word \"the\" %d times.",argv[i],h);
      h=0; 
      fclose(fp);
  }
}

  • 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-18T04:50:42+00:00Added an answer on May 18, 2026 at 4:50 am
     char* mess;
     [...]
           fscanf(fp,"%s",mess);
    

    mess is uninitialised. You need to allocate some space for the word you are reading in

    s Matches a sequence of
    non-white-space characters; the next
    pointer must be a pointer to char, and
    the array must be large enough to
    accept all the sequence and the
    terminating NUL character. The input
    string stops at white space or at the
    maximum field width, whichever occurs
    first.

    so you also want to use the field width to limit what you read to the size of your buffer. This requires a bit of careful handling since the bit after your buffer size just might be “the” (e.g. “breathe”, if you read 4 character words, would give you “brea” and “the” and a false positive)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have a program written in VB6 that reads a long text file and
I have a simple program (written in Java) which uses the google protocol buffer
Hi I have a program written in C++ in which one or two functions
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a program which i have myself written in java, but I want
I am beginning to port a program which is written in C and have
I have a debugging program which I've written to attach to a process and
I have program written in C. It takes 2 arguments username/password and try to
I have a program written in C, which is named computeWeight.c and to compile
I have written a program which works on huge set of data. My CPU

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.