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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:40:51+00:00 2026-06-14T07:40:51+00:00

C++ ifstream get line change getline output from char to string I got a

  • 0

C++ ifstream get line change getline output from char to string

I got a text file.. so i read it and i do something like

char data[50];
readFile.open(filename.c_str());

while(readFile.good())
{
readFile.getline(data,50,',');

cout << data << endl;

}

My question is instead of creating a char with size 50 by the variable name data, can i get the getline to a string instead something like

string myData;
readFile.getline(myData,',');

My text file is something like this

Line2D, [3,2]
Line3D, [7,2,3]

I tried and the compiler say..

no matching function for getline(std::string&,char)

so is it possible to still break by delimiter, assign value to a string instead of a char.

Updates:

Using

while (std::getline(readFile, line))
{
    std::cout << line << std::endl;
}

IT read line by line, but i wanna break the string into several delimiter, originally if using char i will specify the delimiter as the 3rd element which is

readFile.getline(data,50,’,‘);

how do i do with string if i break /explode with delimiter comma , the one above. in line by line

  • 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-14T07:40:52+00:00Added an answer on June 14, 2026 at 7:40 am

    Use std::getline():

    std::string line;
    while (std::getline(readFile, line, ','))
    {
        std::cout << line << std::endl;
    }
    

    Always check the result of read operations immediately otherwise the code will attempt to process the result of a failed read, as is the case with the posted code.

    Though it is possible to specify a different delimiter in getline() it could mistakenly process two invalid lines as a single valid line. Recommend retrieving each line in full and then split the line. A useful utility for splitting lines is boost::split().

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

Sidebar

Related Questions

I have been spending 2 hours trying to get my path string read from
I'm using std::getline() to read lines from an std::istream-derived class, how can I move
I'm writing a program for an exercise that will read data from a file
The cplusplus.com example for reading text files shows that a line can be read
I'm trying to read information from a file and process that information in a
I can't get files in Xcode to be read. I've tried everything from putting
void BinaryTree::InitializeFromFile(string Filename){ ifstream inFile; inFile.open(Filename, fstream::binary); if(inFile.fail()){ cout<<Error in opening file <<Filename; return;
I'm writing a program that will guess words taken from a big text file.
I'm trying to read from a file some data I already have and to
std::ifstream sr(path.c_str()); if (!sr) throw runtime_error(Could not open file '+path+\'); sr.seekg(0, ios::end); streampos lastPos

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.