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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:14:27+00:00 2026-06-01T11:14:27+00:00

I have tried to use the following snippet of code: int main() { string

  • 0

I have tried to use the following snippet of code:

int main()
{
string location_file ("test.txt");
string data;

ifstream file (location_file);
getline (file, data);
file.close();

cout << data;
return 0;
}

But it won’t work. Now, if I were to use “ifstream file (“test.txt”)” it would. Why? Isn’t it the same thing in the end?

  • 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-01T11:14:28+00:00Added an answer on June 1, 2026 at 11:14 am

    The problem is that ifstream‘s constructor does not accept a string as parameter, but only a char const*. Now "test.txt" is of type char const[9] which decays into char const* according to type promotion rules, therefore it works. However, there’s no implicit conversion from string to char const*, therefore your code doesn’t work.

    To fix your code, construct your stream with:

    ifstream file(location_file.c_str());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to use the following code to retrieve the data from the
I have tried to use the following code: cd c:\windows\system32 regsvr32.exe dllname.ax But this
I have tried to use the following code to passing value back from the
As the title says ... I have tried to use the following code to
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
I have the following code snippet. I am compiling using the sun studio 12
I have tried to use the following regex expression to remove html whitespace and
I have following HTML code snippet: <li>2011-10-17</li> <li><p><img style=float: left; alt=photo1 src=/images/stories/site/news/photo1.jpg height=148 width=217>Some
I have tried to use mysql fulltext search in my intranet. I wanted to
I have heard about JNI and have tried to use it. What I would

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.