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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:01:09+00:00 2026-06-05T19:01:09+00:00

I am currently working with a file name and I need to get the

  • 0

I am currently working with a file name and I need to get the name excluding the extension. I have a separate function that determines the extension of the file but can’t find a way how to get the name without the extension.

If I have the file name mylog.txt I only want to be able to get out mylog

I have tried the following

strncpy(fileNameWithoutExt, LogRotateConfiguration->logFileName,
    strlen(LogRotateConfiguration->logFileName-strlen(fileExtension)-1));

For some reason though, when I run this I only get minstead of the rest of the file name. I’m never going to know many characters are within the file name or extension so it needs to be worked out what section it needs to get.

So basically, the question how would I get the name of the file without the extension.

Thanks for any help you can provide.

  • 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-05T19:01:10+00:00Added an answer on June 5, 2026 at 7:01 pm

    Huh?

    Are you seriously subtracting the extension’s length from the pointer you pass to strlen()? That seems dangerous and weird.

    Also, you shouldn’t use strncpy() unless you really need it’s rather quirky semantics. Chances are you don’t, and using it to copy part of a longer string is a pretty good way of failing.

    A better solution is often to figure out the lengths, and use memcpy():

    char         out[WHATEVER];
    const size_t len = strlen(LogRotateConfiguration->logFileName);
    const size_t elen = strlen(fileExtension);
    const size_t out_len = len - elen;
    
    if( out_len < sizeof out )
    {
      memcpy(out, LogRotateConfiguration->logFileName, out_len);
      out[out_len] = '\0';
    }
    else
      out[0] = '\0'; /* Make sure we get an empty output if filename won't fit. */
    

    Note that this also uses const where appropriate, and stores the return value of strlen() in variables of the proper type, i.e. size_t.

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

Sidebar

Related Questions

I`m currently working on a script, and I have the following situation. function somnicefunction()
Im currently working on a site that you can upload large files.. on local
Im currently working on a project that requires the following. I need to be
I have some text configuration file that need to be read by my program.
I'm currently working on a project that involves a lot of text file reading.
Am currently working on an application that requires users to submit posts and comments
I have a p:fileUpload which works fine. But I need a partial page rendering
I am working on PIL and need to know if the image quality can
I have the following git status and I need to get rid of pluginA
Quite often I need to get a simple text copy of my currently opened

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.