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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:33:04+00:00 2026-05-14T14:33:04+00:00

I want to throw the last three character from file name and get the

  • 0

I want to throw the last three character from file name and get the rest?

I have this code:

char* remove(char* mystr) {

    char tmp[] = {0};
    unsigned int x;

    for (x = 0; x < (strlen(mystr) - 3); x++)
        tmp[x] = mystr[x];

    return tmp;
}
  • 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-14T14:33:04+00:00Added an answer on May 14, 2026 at 2:33 pm

    Try:

    char *remove(char* myStr) {
        char *retStr;
        char *lastExt;
        if (myStr == NULL) return NULL;
        if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
        strcpy (retStr, myStr);
        lastExt = strrchr (retStr, '.');
        if (lastExt != NULL)
            *lastExt = '\0';
        return retStr;
    }
    

    You’ll have to free the returned string yourself. It simply finds the last . in the string and replaces it with a null terminator character. It will handle errors (passing NULL or running out of memory) by returning NULL.

    It won’t work with things like /this.path/is_bad since it will find the . in the non-file portion but you could handle this by also doing a strrchr of /, or whatever your path separator is, and ensuring it’s position is NULL or before the . position.


    A more general purpose solution to this problem could be:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // remove_ext: removes the "extension" from a file spec.
    //   myStr is the string to process.
    //   extSep is the extension separator.
    //   pathSep is the path separator (0 means to ignore).
    // Returns an allocated string identical to the original but
    //   with the extension removed. It must be freed when you're
    //   finished with it.
    // If you pass in NULL or the new string can't be allocated,
    //   it returns NULL.
    
    char *remove_ext (char* myStr, char extSep, char pathSep) {
        char *retStr, *lastExt, *lastPath;
    
        // Error checks and allocate string.
    
        if (myStr == NULL) return NULL;
        if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
    
        // Make a copy and find the relevant characters.
    
        strcpy (retStr, myStr);
        lastExt = strrchr (retStr, extSep);
        lastPath = (pathSep == 0) ? NULL : strrchr (retStr, pathSep);
    
        // If it has an extension separator.
    
        if (lastExt != NULL) {
            // and it's to the right of the path separator.
    
            if (lastPath != NULL) {
                if (lastPath < lastExt) {
                    // then remove it.
    
                    *lastExt = '\0';
                }
            } else {
                // Has extension separator with no path separator.
    
                *lastExt = '\0';
            }
        }
    
        // Return the modified string.
    
        return retStr;
    }
    
    int main (int c, char *v[]) {
        char *s;
        printf ("[%s]\n", (s = remove_ext ("hello", '.', '/'))); free (s);
        printf ("[%s]\n", (s = remove_ext ("hello.", '.', '/'))); free (s);
        printf ("[%s]\n", (s = remove_ext ("hello.txt", '.', '/'))); free (s);
        printf ("[%s]\n", (s = remove_ext ("hello.txt.txt", '.', '/'))); free (s);
        printf ("[%s]\n", (s = remove_ext ("/no.dot/in_path", '.', '/'))); free (s);
        printf ("[%s]\n", (s = remove_ext ("/has.dot/in.path", '.', '/'))); free (s);
        printf ("[%s]\n", (s = remove_ext ("/no.dot/in_path", '.', 0))); free (s);
    
        return 0;
    }
    

    and this produces:

    [hello]
    [hello]
    [hello]
    [hello.txt]
    [/no.dot/in_path]
    [/has.dot/in]
    [/no]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to throw an exception in my constructor so that I don't have
I want to throw an exception to say that we have an invalid email
Suppose you want to throw Exception like this: 'Project with the provided ID cannot
I have this code: catch (Exception e) { try { transmitModel.AddAck(transmitBatchId, <error><message> + e.Message
I want to retreive all sub directories from a root directory until the last
I have been digging since last three days to find out the appropriate way
Below is my code and on the last line of code I get a
i want to have a xml file in the user specifed directory. for that
We have a site that contains streaming video and we want to display three
I have a data.table in R where I want to throw away the first

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.