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

  • Home
  • SEARCH
  • 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 9206181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:07:17+00:00 2026-06-18T00:07:17+00:00

I cannot figure out why getline is working in one X-Code project but not

  • 0

I cannot figure out why getline is working in one X-Code project but not in another. The error “No matching function for call to ‘getline’.
When I make a single cpp file it compiles with no issues.

  // reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while ( myfile.good() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }

  else cout << "Unable to open file"; 

  return 0;
}

I would really appreciate some assistance. I am just learning and the example above came from my test book.

  • 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-18T00:07:18+00:00Added an answer on June 18, 2026 at 12:07 am

    The code is noisy and incorrect. The correct standard idiom is like this:

    #include <fstream>   // for std::ifstream
    #include <string>    // for std::getline and std::string
    
    std::ifstream myfile("example.txt");
    
    if (!myfile) { /* error, die */ }
    
    for (std::string line; std::getline(myfile, line); )
    {
        std::cout << "Read one line: '" << line << "'\n";
    }
    

    Correctness:

    • You must check the success of the input operation before consuming the input. To do otherwise may be UB, and certainly never correct.

    • [Thanks @James for pointing this out:] good() doesn’t check if a file was opened. You could use either !myfile.fail() or myfile.is_open(), but just don’t bother (see below).

    Noise:

    • The ifstream constructor takes the filename and opens the file already. Use it.

    • The ifstream cleans up in its destructor, no need to do that explicitly. Use tight scoping to close the file as soon as you’re done with it.

    • Don’t leak line into the ambient scope if you don’t need it.

    • No need for good() (or any of the correct alternatives). Just evaluate the ifstream object in a boolean context to see if the file was opened successfully.

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

Sidebar

Related Questions

I cannot figure out why this is not working. Can anyone explain to me
I cannot figure out why my array is not working, I have $carrier coming
I cannot figure out where the syntax error is in this line of code.
I cannot figure out why these 2 buttons are not working, i have a
I cannot figure out why this is not working. I will put up all
I cannot figure out why my calculation is not working. I am doing for
I cannot figure out why I get this error during check-in. I checked in
I cannot figure out how to convert this code from C# to VB.net. It
I cannot figure out how to get rid of errors that should not be
I cannot figure out how to get this code to work properly. I am

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.