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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:13:19+00:00 2026-05-25T13:13:19+00:00

I need some help with a program that I am writing for my Systems

  • 0

I need some help with a program that I am writing for my Systems Programming class. It is in C and I have very, very little experience with C. I need to merge three text file with the format:

word1
word2
word3
...
wordX

I am also to bring each of the words from all three files and put them into a 2D array (an array of string-arrays), then use some sort of sorting method on them.

I shouldn’t need help with the sorting, but I don’t know how to get the word count from each of the text files or put them into an array.


This is the function I have for counting the words in the file. It doesn’t compile on gcc (probably for obvious reasons, but I don’t know them). Do I even have the right idea?

int countWords(FILE f){
   int count = 0;
   char ch;
   while ((ch = fgetc(f)) != EOF){
       if (ch == '\n')
           count++;
       //return count; originally here, but shouldn't be.
   }
       return count;
}

EDIT: I supposed I could just find a way to count the lines in the program, but I’m not sure if the approach would be any different from what I am trying to do here. (I have never really been that good at working with text files.


I got it to count all of the lines in the program. I guess I’m a little rusty.


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

int countWords(FILE *f){
   int count = 0;
   char ch;
   while ((ch = fgetc(f)) != EOF){
       if (ch == '\n')
           count++;
   }
   return count;
}
int main(void){

   int wordCount = 0;
   FILE *rFile = fopen("american0.txt", "r");
   wordCount += countWords(rFile);
   printf("%d", wordCount);
   return 0;
}

I kind of forgot about that the pointer thing with FILE *fileName

  • 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-25T13:13:19+00:00Added an answer on May 25, 2026 at 1:13 pm

    The type you use for a file in c is FILE*. That star is important, indicating that the type is a “pointer to FILE”. It is unlikely that countWords(FILE f) is what you meant to write.

    Each time you call your function, it will have a fresh count = 0, so it will always return 0 or 1. Try using static int count;, making count a global variable, or passing in the current count to the function. Your other option is to move the return count; line outside of the while loop.

    You will also probably need to divide the count by two to get the number of words, using the format you posted.

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

Sidebar

Related Questions

I need some help with a program I'm writing for my Programming II class
Need some help about with Memcache. I have created a class and want to
Im still a beginner in C programming and I need a little help writing
I'm writing a C++ program and I need some help understanding an error. By
I need some help making this program for class. We are working with g++
Ok, I need some help here. I'm attempting to write a program that rolls
I need some help calculating Pi. I am trying to write a python program
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded

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.