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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:56:27+00:00 2026-06-17T13:56:27+00:00

FILE *file = fopen(argv[1], r); if(file != NULL){ char command[MAX_BUFFER]; while(fgets(command, MAX_BUFFER, file) !=

  • 0
    FILE *file = fopen(argv[1], "r");
    if(file != NULL){
        char command[MAX_BUFFER];
        while(fgets(command, MAX_BUFFER, file) != NULL){ //read line
            //operations
        }
        fclose(file);
    }
    else write(fileno(stderr), ERROR_MESSAGE, strlen(ERROR_MESSAGE));

I’m working on a pretty basic UNIX shell implementation that reads and executes line by line from a file. From the above code, I’m trying to figure out why fclose() might fail since valgrind seems to be telling me that I’ve left this file descriptor open.

Am I correct in assuming that the “still reachable” 568 bytes in the leak summary refer to a failed fclose() somehow?

==25428== FILE DESCRIPTORS: 4 open at exit.
==25428== Open file descriptor 3: test
==25428==    at 0x4F186B0: __open_nocancel (syscall-template.S:82)
==25428==    by 0x4EAC628: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:233)
==25428==    by 0x4EA1265: __fopen_internal (iofopen.c:93)
==25428==    by 0x400C0C: main (in /home/Desktop/sh)
==25428== 
==25428== Open file descriptor 2: /dev/pts/0
==25428==    <inherited from parent>
==25428== 
==25428== Open file descriptor 1: /dev/pts/0
==25428==    <inherited from parent>
==25428== 
==25428== Open file descriptor 0: /dev/pts/0
==25428==    <inherited from parent>
==25428== 
==25428== LEAK SUMMARY:
==25428==    definitely lost: 0 bytes in 0 blocks
==25428==    indirectly lost: 0 bytes in 0 blocks
==25428==      possibly lost: 0 bytes in 0 blocks
==25428==    still reachable: 568 bytes in 1 blocks
==25428==         suppressed: 0 bytes in 0 blocks
  • 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-17T13:56:28+00:00Added an answer on June 17, 2026 at 1:56 pm

    Your visible code looks ok.
    But to answer your question: likely, but the problem is somewhere else 😉

    To debug your program, comment out ‘operations’, so you have an empty loop.
    Does valgrind still complain ?

    No: the problem very likely lies within the code you named ‘operations’.

    Remove the comments and compile with debugging symbols on (-g) and use valgrind with these options:
    valgrind –leak-check=yes –show-reachable=yes –track-origins=yes yourprogram

    Yes: (very unlikely) something else got smashed, try to valgrind with more commented code.

    To find bugs at compile time, use the compiler’s flags (e.g. gcc has -Wall -Wextra -Wwrite-strings -Wformat=2 -Wformat -Wformat-security -D_FORTIFY_SOURCE=2 and more).

    And make use of static analysers e.g. llvm/clang (compile with clang –analyze yourprogram.c)

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

Sidebar

Related Questions

I tried to create a file using Oracle UTL_FILE.FOPEN command but i get this
I am having trouble figuring out how to write to a file using fopen
Using PHP, it's possible to read off the contents of a file using fopen
I'm using fopen to read from a file $fh = fopen($path, 'r') or die('Could
In C++, I write a float image into a file: FILE* fp = fopen(image.fft,
Depending on command-line arguments, I'm setting a file pointer to point either towards a
I had a terrible time with file input from command line arguments last semester
I want to read a txt file line by line. I am getting the
I need to write a program that create pipe send filename from command line
I read the following file (file.txt) line by line: 1 -5 6 -8 -33

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.