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

  • Home
  • SEARCH
  • 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 9248863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:03:01+00:00 2026-06-18T10:03:01+00:00

Basically I’m supposed to make a program that copies the Unix -wc command. Flags

  • 0

Basically I’m supposed to make a program that copies the Unix -wc command. Flags -l, -w, -c, and -L are supposed to display # of lines, # of words, # of characters, and # of characters in line, respectively.

I am having trouble reading in a text file (first time doing it in C). I used GDB and discovered my problem lies in reading in the file. After awhile it just reads in null characters for whatever reason.

Please assume everything is right with my code except for reading in the file.

Here is my code:

void readInFile(char** argv, int arg, int addFlags, int argc)
{
   FILE *myFile;
   char c;
   int wordCount = 0, lineCount = 1, longestLine, characterAmount = 0;
   int charactersInLine = 0;

   myFile = fopen(argv[arg], "r");
   if(!myFile)
   {
      printf("%s not found!", argv[arg]);
      exit(EXIT_FAILURE);
   }

   while(c != EOF)
   {
      c = fgetc(myFile);
      putchar(c);
      characterAmount++;
      charactersInLine++;

      if(c == ' ')
         wordCount++;
      if(c == '\n')
      {
      if(charactersInLine > longestLine)
         longestLine = charactersInLine;
      charactersInLine = 0;
      lineCount++;
      wordCount++;
   }
}

Thanks for you time!

  • 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-18T10:03:03+00:00Added an answer on June 18, 2026 at 10:03 am

    This is becoming one of the most common problems, heh.

    You’re missing that fgetc() returns int, not char. This is because EOF is not a valid character, so a larger type is needed. See this documentation, for instance.

    You’re also testing c before assigning it for the first time, which basically makes your code rely on whatever happens to be in the uninitialized variable. This is a problem.

    Also, you should probably test for EOF before counting.

    Further, it’s considered a bad idea to use magic numbers in code.

    This:

    if(c == 32)
    

    can be written:

    if(c == ' ')
    

    and this:

    if(c == 10)
    

    can be:

    if(c == '\n')
    

    which are both clearer, in my opinion.

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

Sidebar

Related Questions

Basically I need to verify that a certain program is not running before installation.
Basically, I have a UIImageView that will loop through 8 PNGs over 0.5 seconds.
Basically I've made a form in Cakephp 2.1 with a jQuery button that appends
Basically from C++ FAQ I learned that: A virtual function allows derived classes to
Basically I am trying to make the in-app purchase work, but with no luck.
Basically what I've done is making a simple animation that moves a rectangle the
Basically, from the form that I made I send customer_name and with this code
Basically I wrote a script that generates a xml file based on user input.
Basically I just need the effect of copying that HTML from browser window and
Basically, I have an object that I think should be garbage-collected but it's not.

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.