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

The Archive Base Latest Questions

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

I have just done what appears to be a common newbie mistake : First

  • 0

I have just done what appears to be a common newbie mistake:

First we read one of many tutorials that goes like this:

 #include <fstream>
 int main() {
      using namespace std;
      ifstream inf("file.txt");
      // (...)
 }  

Secondly, we try to use something similar in our code, which goes something like this:

#include <fstream>
int main() {
    using namespace std;
    std::string file = "file.txt"; // Or get the name of the file 
                                   // from a function that returns std::string.
    ifstream inf(file);
    // (...)
}

Thirdly, the newbie developer is perplexed by some cryptic compiler error message.

The problem is that ifstream takes const * char as a constructor argument.

The solution is to convert std::string to const * char.

Now, the real problem is that, for a newbie, “file.txt” or similar examples given in almost all the tutorials very much looks like a std::string.

So, is “my text” a std::string, a c-string or a *char, or does it depend on the context?

Can you provide examples on how “my text” would be interpreted differently according to context?

[Edit: I thought the example above would have made it obvious, but I should have been more explicit nonetheless: what I mean is the type of any string enclosed within double quotes, i.e. “myfilename.txt”, not the meaning of the word ‘string’.]

Thanks.

  • 1 1 Answer
  • 1 View
  • 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:05:31+00:00Added an answer on May 16, 2026 at 12:05 pm

    So, is “string” a std::string, a c-string or a *char, or does it depend on the context?

    • Neither C nor C++ have a built-in string data type, so any double-quoted strings in your code are essentially const char * (or const char [] to be exact). “C string” usually refers to this, specifically a character array with a null terminator.
    • In C++, std::string is a convenience class that wraps a raw string into an object. By using this, you can avoid having to do (messy) pointer arithmetic and memory reallocations by yourself.
    • Most standard library functions still take only char * (or const char *) parameters.
    • You can implicitly convert a char * into std::string because the latter has a constructor to do that.
    • You must explicitly convert a std::string into a const char * by using the c_str() method.

    Thanks to Clark Gaebel for pointing out constness, and jalf and GMan for mentioning that it is actually an array.

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

Sidebar

Related Questions

I have done Objective-C way back when, and have recently (i.e. just now) read
My latest problem is one that I already have a solution for, it just
I have just done something without thinking it through. I have made a branch
I have just started using jQuery and although following code gets the job done,
I've never done this before. The website I have just built is the second
I have done this operation millions of times, just using the + operator! I
I almost done my coding for a specific program i have. I just need
I have just discovered a variable name that is misspelled. If it were hidden
I have just recently battled a bug in Python. It was one of those
I have just optimised some Ruby code that was in a controller method, replacing

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.