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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:50:59+00:00 2026-05-25T16:50:59+00:00

My problem is the following: a const char* is provided to the function in

  • 0

My problem is the following:

  • a const char* is provided to the function in which I would like to perform the operation I am going to describe
  • what is provided is a filename (full absolute path included) of indeterminate length
  • I would like to locate the occurrence of the substring output and replace it with input
  • the output of these operations must be another const char* (too much code to change to replace it with a std::string)

What I was thinking to do is the following

string name(filename); //filename is the "const char*" provided by the caller of the function
string portion("output");
name.replace(name.find(insert),insert.length(),"input");
const char* newfilename = (char*)name.c_str();

Now, my questions:

  • would do this work?
  • is there a better way to obtain what I need?

Thanks to anyone that will help.

Federico

  • 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-25T16:51:00+00:00Added an answer on May 25, 2026 at 4:51 pm

    This will work, but you don’t need the cast on name.c_str() (in fact, it’s wrong: c_str() returns a const char *).

    But the pointer you get from name.c_str() is invalidated as soon as you modify name, or when name goes out of scope. So don’t try returning newfilename from a function, for instance.

    If you need it to persist, you have no option but to dynamically allocate memory. Standard practice would be to use a smart pointer to automatically manage deallocation.const char *, you have no option but to manage this yourself. So you could do:

    char *newfilename = new char[name.length() + 1];
    strcpy(newfilename, name.c_str());
    return newfilename;
    ...
    
    delete [] newfilename;
    

    * Well, standard practice would be to use a std::string! It only gets tricky if you need to interface with a legacy C API.

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

Sidebar

Related Questions

I have the following function: int strpos(const char *needle, const char *haystack) { int
I've following problem, which I've not been able to do successfully. Your help will
Consider the following problem: A multi-line string $junk contains some lines which are encoded
Suppose we have the following method (it is in c code): const char *bitap_search(const
given the following code: /* signatures */ int getParams(char params[MAX_PARAM_LEN][MAX_LINE_LEN]); int getVersion(const char params[MAX_PARAM_LEN][MAX_LINE_LEN],
Consider the following simple map: class MyCoolMap : public unordered_map<const char *, const char
The following code, in global scope, doesn't compile: const char *one = 1; const
Why does the following code in C work? const char* str = NULL; str
Consider the following code: class CString { private: char* buff; size_t len; public: CString(const
I have the following code: const char* argument = string1.c_str(); QString arg(argument); QProcess *proc

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.