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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:15:11+00:00 2026-06-18T16:15:11+00:00

For my assignment I have to create a program similar to the -wc unix

  • 0

For my assignment I have to create a program similar to the -wc unix command which counts words, lines, etc.

I have to read in flags and read in a text file.

I’ve set up all the flags and now I’m trying to read in a text file. I don’t think I’m doing this right.

void readInFile(char** argv, int arg)
{
   FILE *myFile;
   char c;

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

in my main I call the function readInFile() and pass 2 arguments. Argv and the element where the file should be. So assume this to be correct.

I need help with actually opening up the file. I feel like my fopen() is wrong. I’m new to reading/writing files in C. Thanks alot!

  • 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-18T16:15:12+00:00Added an answer on June 18, 2026 at 4:15 pm

    I’m going to give you some general advice here.

    Usually functions should do a single job. In this case, you are writing a function to read in a single file. So, don’t pass a pointer to all the command-line arguments; pass in a single read-only pointer to the name of the file to open. Then in main() select the correct argument and pass that as the argument.

    void readInFile(char const *filename)
    

    Now, if this function will be reading in the file and doing nothing else, it needs to return the data somehow. But if this function will be doing the equivalent of wc, maybe it will read the file and print stuff, not return any data to the main() function. Then maybe the name should be improved:

    void wordcount(char const *filename)
    

    The actual call to fopen() looks fine to me.

    You check for error, and then call exit() immediately. That’s one way to do it. Another way to do it is to return an error code from your function, and have the caller (the main() function) check for failure, and handle the error there.

    int wordcount(char const *filename)
    {
        // ... do stuff
        if (failed)
            return 1;  // return nonzero error code on failure
        // ... do more stuff
        return 0;  // success code
    }
    
    int main(int argc, char const **argv)
    {
        char const *filename;
        int result;
    
        filename = argv[1];
        result = wordcount(filename);
        if (result)
        {
            fprintf(stderr, "unable to open file '%s'\n", filename, result);
            exit(result);
        }
        return 0;
     }
    

    For a program this simple, it doesn’t matter much. But once you start building larger systems in software, you will be happier if your functions work well together, and part of that is making functions that return error codes rather than terminating your whole program on any error.

    Why am I using 0 for the success code, and non-zero for failure? It’s a common way to do it. It’s easy to test for non-zero, like if (result) and there are many non-zero codes but only one zero, so you can return many different kinds of errors, but there is only one value needed for “success”.

    Note that instead of calling exit() from main(), you can just use the return statement. When you return 0 from main(), that signals success, and a non-zero value indicates an error. So you could just use return result; from main() if you like.

    In my dummy code, I’m just returning 1 as the error code. But actually, when you call fopen() it returns an error code to you, in a global variable called errno. Probably a better option is to make your function return the actual error code as specified in errno. You could even modify the print statement in the main() function print the errno code, or use the strerror() function to turn that error code into a human-readable message.

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

Sidebar

Related Questions

I have an assignment in which i have to create a console program in
For a school assignment I have to create a C++ program that will create
I have an assignment to create a program that prompts user input of an
Have a homework assignment in which I'm supposed to create a vector of pointers
I have a programming assignment and we are asked to create a program in
I have an assignment in college in which we are required to read in
I have an assignment for uni where I have to create a Java program
I have an assignment to create a program that converts deci (-32,768 through 32,767)
I have an assignment to create a paint program in Java. I have managed
For a homework assignment, I have to create a simple program that takes two

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.