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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:22:20+00:00 2026-05-18T01:22:20+00:00

My goal is to gather input and open files based on that input. FILE*

  • 0

My goal is to gather input and open files based on that input.

FILE* 
open_input_file (char* fileName) //opens source file to be read
{ 
 return fopen(fileName, "r");
}

In an earlier function, I collect input from the user and save it to fileName. When I debug the program, it tells me fopen is returning NULL. That’s not what I want, and I’m not sure where the problem is.

int main(void)
{    FILE* inFile = NULL;
     char infileName[32] = {'\0'};
     gather_input(infileName); // infileName is an output parameter for this
     inFile = open_input_file(infileName);
}

I don’t know what the problem is. Any thoughts?

  • 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-18T01:22:20+00:00Added an answer on May 18, 2026 at 1:22 am

    If fopen returns NULL, the open failed. errno will hold the failure code and strerror(errno) will return a short description of why the open failed.

    #include <errno.h>
    #include <string.h>
    
    ...
    
    int main(void)
    {    FILE* inFile = NULL;
         char infileName[32] = {'\0'};
         gather_input(infileName); // infileName is an output parameter for this
         if (!(inFile = open_input_file(infileName))) {
             fprintf(stderr, "Error opening '%s': %s\n", 
                     infileName, strerror(errno));
         } else {
             // open successful
             ...
         }
    }
    

    Off-topic

    gather_input better make sure infileName is null-terminated to prevent buffer overflows. The simplest way to do this is to define the size of the file name buffer as a macro and set the last character to 0.

    #define FILENAMELEN 32
    void gather_input(char infileName[]) {
        ...
        infileName[FILENAMELEN-1]=0;
    }
    
     int main(void)
    {    FILE* inFile = NULL;
         char infileName[FILENAMELEN] = {'\0'};
    

    This isn’t very flexible. You could instead pass the size of the file name buffer into gather_input.

    #define LENGTH(a) (sizeof(a) / sizeof(a[0]))
    void gather_input(char infileName[], size_t len) {
        ...
        infileName[len-1]=0;
    }
    
     int main(void)
    {    FILE* inFile = NULL;
         char infileName[32] = {'\0'};
         gather_input(infileName, LENGTH(infileName)); // infileName is an output parameter for this
    

    An alternative to setting the last character, if using standard string manipulation functions, is to use the strl* functions (strlcpy and strlcat) rather than their unbounded cousins. If you aren’t using strl*, you should be using strncpy and strncat.

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

Sidebar

Related Questions

My goal is to maintain a web file server separately from my main ASP.NET
my goal is to write a stored proc that can collect all field values
The goal: Any language. The smallest function which will return whether a string is
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
My Goal I would like to have a main processing thread (non GUI), and
My goal here is to create a very simple template language. At the moment,
The goal: To create a .NET dll i can reference from inside SQL Server
My goal is to recognize simple gestures from accelerometers mounted on a sun spot.
My goal is to integrate testing into my development environment (as post-build step). I

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.