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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:09:46+00:00 2026-06-06T16:09:46+00:00

I have a c program that is trying to read in a file. Using

  • 0

I have a c program that is trying to read in a file. Using the access() command it says the file is there but fopen() returns NULL and errno says the file does not exist.

A truncated snippet of code:

  FILE *fp;
  char *filename = strdup(git_dir);
  strcat(filename, "/HEAD");

  printf(git_dir);
  printf(":");
  printf(filename);
  printf(":");

  if (access(filename, F_OK)) {
    printf("Y U NO OPEN:");
  }

  fp = fopen(filename, "r");
  if (fp == NULL) {
    printf(strerror(errno));
    return;
  }

As you might tell from the code, this is trying to open the .git/HEADS file of a git repository. The particular repository this is failing on was cloned into a subdir of another repository and then added as a submodule. I do not have problems that were cloned by running the git submodule update command.

The above code does not print “Y U NO OPEN:” but it does print out the strerror(). I printed the filename to the screen and opened the file with less and it opens fine. This program is generating my zsh prompt, so I imagine it is being run by my user, and the permissions on the file are

-rw-r--r-- 1 ben users   23 Jun 30 13:32 HEAD

Any suggestions?

  • 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-06T16:09:48+00:00Added an answer on June 6, 2026 at 4:09 pm

    You can’t do this:

    char *filename = strdup(git_dir);
    strcat(filename, "/HEAD");
    

    You’re appending the string “/HEAD” to filename, but filename only has room for the content of git_dir. Thus you’re writing past the buffer, overwriting memory, and possibly causing havoc, and all kinds of unpredictable behavior can occur.

    Do this instead:

    char *filename = malloc(strlen(git_dir) + 6); //length of /HEAD + null terminator
    strcpy(filename, git_dir);
    strcat(filename, "/HEAD");
    

    Keep in mind that stdout is normally line-buffered as well. This means that if you do printf("Y U NO OPEN:"); , you might not see the output immediately. Print a newline, \n, to flush the output.

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

Sidebar

Related Questions

I have a program that reads input from a file. I am trying to
I have an XML file that I'm trying to read from here , and
I'm trying to read lines from a SIC/XE program using a C program that
I'm trying to execute a program on a file using the popen() command on
I have a small program that I'm trying to create to get ip addresses
I am trying to polish up a program a program that I have converted
I have been trying, in vain, to make a program that reads text out
I'm trying to multithread something and i have my program set up such that
Background I have a Spring batch program that reads a file (example file I
I'm trying to write a secure transfer file program using Python and AES and

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.