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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:37:58+00:00 2026-06-02T12:37:58+00:00

let me first thank you for taking the time to read this. I’m trying

  • 0

let me first thank you for taking the time to read this.

I’m trying to read a file in c++. Currently I have a method that allows the user to select a file in explorer and returns this as an ‘std::string’. I then have to open this file, but the method I have for this uses const char*. Therefore I need to convert from one to the other.

If there is an easy method in windows for reading a file using a string instead then let me know as it would solve my entire problem.

When I convert from string to const char*, using str.c_str(), I get a lot of weird characters at the end. I’ve researched other topics with the same problem, but the answers all seem very specific for those projects, or say just stick to sting/vector instead. Obviously I would happily do this, but I don’t have a method that opens the file using a string/vector.

Any help is appreciated 🙂 The code and output of where this occurs is pasted below.

*SOURCE

std::string f;

               if (LOWORD(wParam) == 1) {

                       f = openFile();

                       char *fchar = new char[f.size()+1]; // +1 to account for \0 byte

                       //char *fchar = std::vector[1];

                       std::strncpy(fchar, f.c_str(), f.size());

                       file1 = fchar;

                       std::cout<<"string size: " << f.size() << std::endl;
                       std::cout<<"string: " << f << std::endl;
                       std::cout<<"fchar: " << fchar << std::endl;
                       std::cout<<"file1: " << file1 << std::endl;
               }

OUTPUT

 string size: 33
 string: C:\Users\Joseph\Pictures\back_raw
 fchar: C:\Users\Joseph\Pictures\back_raw═²²²²½½½½½½½½¯■
 file1: C:\Users\Joseph\Pictures\back_raw═²²²²½½½½½½½½¯■**
  • 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-02T12:38:02+00:00Added an answer on June 2, 2026 at 12:38 pm

    std::strncpy() does not place a `\0′ character at the end of the string. See http://www.cplusplus.com/reference/clibrary/cstring/strncpy/. You need to copy that as well:

    std::strncpy(fchar, f.c_str(), f.size() + 1);

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

Sidebar

Related Questions

First off, let me thank you if you're taking the time to look at
First let me say I have read this useful article thoroughly and am using
First of all, thanks for taking the time to read this. I've been working
Thank you for taking the time to read this. I looked over Stack Overflow
I have a question, but let me first say that this is being performed
Let me first explain what's I'm trying to do. So I have a entity
this is my first time using any StackExchange website, so let's see how it
Thanks for viewing my question. Let me describe the app first. I have a
Let me first describe my goal: I have created an object with 3 properties:
First let me mention that I've gone through many suggested questions and found no

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.