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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:45:22+00:00 2026-06-06T01:45:22+00:00

I am working on a c project and I need to remove a file

  • 0

I am working on a c project and I need to remove a file from within a directory. For some reason though it keeps on saying that it can’t delete because the file or directory doesn’t exist. Below is the code that I am using to remove the file.

void deleteOldestLog()
{
    FILE *fp;
    char path[FILE_PATH_BUF_LEN], *fileName;

    fp = popen("ls -tr /home/myfolder/logs/ |head -1", "r");
    if (fp == NULL)
    {
        printf("Failed to run command");
    }
    else
    {
        char removalPath[FILE_PATH_BUF_LEN];
        while ((fileName = fgets(path, sizeof(path)-1, fp)) != NULL)
        {
            sprintf(removalPath, "/home/myfolder/logs/%s", fileName, sizeof(fileName)-1);

            printf("Removing file: %s", removalPath);
            if (remove(removalPath) != 0)
            {
                perror("ERROR DELETING LOG");
            }
            else
            {
                printf("Successfully deleted %s", removalPath);
            }
            break;
        }
        pclose(fp);
    }
}

Even though it says that it can’t find the file because it doesn’t exist I know that this isn’t true because if I run ll followed by the path that the c program printed it returns the file that I am trying to delete.

I think it might be because fgets is putting ‘\0’ on the end of the string which is stopping the remove from working.

How can I fix this?

  • 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-06T01:45:24+00:00Added an answer on June 6, 2026 at 1:45 am

    There’s a newline at the end of the file name read by fgets(). Your file name doesn’t actually end with a newline.

    You attempt to remove the newline with:

    sprintf(removalPath, "/home/myfolder/%s", fileName, sizeof(fileName)-1);
    

    However, to be effective, you’d need to use strlen() instead of sizeof(), and you’d need to modify the format string:

    sprintf(removalPath, "/home/myfolder/%.*s", (int)strlen(fileName)-1, fileName);
    

    The argument for the * must be an int and strlen() returns a size_t; hence the cast. (GCC will warn about that sort of thing if you turn on the warnings; use at least -Wall.)

    A tip for you: when in doubt, print the string. I’ll typically use a format like this. Note the angle brackets around the string:

    printf("Removing: <<%s>>\n", removalPath);
    

    When you see:

    Removing: <</home/myfolder/something
    >>
    

    you know there’s a problem with a newline in the string. Without the markers, you might not notice that there’s a newline in the string causing the extra newline in the output.


    Why does the format string need to be modified?

    Let’s look at the original sprintf() again:

    sprintf(removalPath, "/home/myfolder/%s", fileName, sizeof(fileName)-1);
    

    The format string expects 1 argument, a string. The call provides two values, a string and a length. So, the first problem is that there is a left-over argument. This usually does no damage, but be aware of it. Presumably, the reason for passing the length minus one was to lose the last character. The formats in the printf() family can be adorned with one or two numbers, and either or both can have a * instead of an integer value. These numbers constrain the lengths of the formatted value. When you write:

    %.*s
    

    you state the length of the output shall be exactly the length specified by an int value passed as an argument before the string itself. Hence the revision:

    sprintf(removalPath, "/home/myfolder/%.*s", (int)strlen(fileName)-1, fileName);
    

    (which I just fixed while adding this information.)

    I’ve also not added error checking to the output of sprintf() etc. That’s not unusual; however, best coding practices do ensure that functions like sprintf() return the value you expect (which is the number of characters written to the string, excluding the trailing null '\0'.

    (Aside: in general, it is better to use snprintf() than sprintf(); that can avoid buffer overflows.

    snprintf(removalPath, sizeof(removalPath), "/home/myfolder/%.*s",
             (int)strlen(fileName)-1, fileName);
    

    However, the behaviour of the *snprintf() functions under MSVC is different from the behaviour mandated by the C Standards (C99, C11). Worse, in the case of vsnprintf_s() and the other _s functions, the argument lists are different between the MSVC and the C Standard.)

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

Sidebar

Related Questions

I'm working on a project where I need to create a link that when
I'm working on a project that I need to eliminate all references to the
I'm working on an installer project that consists of an MSI file and a
I am working on a small project and I need some help. I have
I am working on a project that uses a queue that keeps information about
I am working on a project and I need to save user configuration. The
I am working on this project where I need to read in a lot
I'm working on a project where I need to get the total row count
I am working on a project where I need to recursively iterate through the
I am working on a prototype project and need to put a new layer

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.