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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:27:43+00:00 2026-05-16T12:27:43+00:00

I have const char* FilePathName which looks like this: C:\ImportantFile.hex And an int id

  • 0

I have const char* FilePathName which looks like this: C:\ImportantFile.hex
And an int id = 12345;
I need to define a new const char* FilePathName_ID that would append the id with an underscore to the original FilePathName to look like this: C:\ImportantFile_12345.hex
I have looked at this but its different as I am using const char* which gives me the error cannot convert from 'const char * ' to 'char'and need an underscore.
I need to end up with a const char*
EDIT I need to keep the file extension.

  • 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-16T12:27:43+00:00Added an answer on May 16, 2026 at 12:27 pm

    You need to create a new std::string object or a null-terminated byte string. One easy way is this:

    std::string append_number(std::string const& x, unsigned int num, char sep = '_') {
        std::stringstream s;
        s << strip_extension(x) << sep << num;
        return s.str();
    }
    

    You can pass a string literal to the above function seamlessly.

    Update: I notice that you probably also need to strip the extension:

    std::string strip_extension(std::string x, char ext_sep = '.') {
         return x.substr(0, x.find_last_of(ext_sep));
    }
    
    std::string get_extension(std::string const& x, char ext_sep = '.') {
         return x.substr(x.find_last_of(ext_sep) + 1); // no error checking 
    }
    

    See updated definition of append_number.

    Update 2: Try the following program:

    #include <string>
    #include <iostream>
    #include <sstream>
    
    std::string strip_extension(std::string const& x, char ext_sep = '.') {
         return x.substr(0, x.find_last_of(ext_sep));
    }
    
    std::string append_number(std::string const& x, unsigned int num, char sep = '_') {
            std::stringstream s;
            s << strip_extension(x) << sep << num << '.' << get_extension(x);
            return s.str();
        }
    
    int main() {
      std::cout << append_number("file.hex", 45) << std::endl;
    }
    

    The output should be:

    file_45.hex
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a const char which is made by concatenation like this: const
I have a piece of C code that looks like this: const char (*foo)[2]
I have a simple C function which I declare as: int strLen(const char* str_)
Say I have defined a variable like this (C++): static const char str[] =
I have a const char* variable which may have a value like OpenStack:OpenStack1. I
I have a string const char[15] and I want to print it like this:
I have this code: int indexOf(const char *array[], char *e) { printf(inside: %d\n,(int)sizeof(array)); /*
I have a file structure like this: file1.h extern const char *build_info[][3]; file1.cpp #include
I have the following string: const char *str = \This is just some random
I have a string as const char *str = Hello, this is an example

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.