Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: getline function with string argument


This question is answered.


Permlink Replies: 2 - Last Post: Oct 25, 2016 10:41 AM Last Post By: Remy Lebeau (Te...
bruce varley

Posts: 19
Registered: 1/31/14
getline function with string argument  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2016 12:33 AM
I can make getline do a file read OK if the buffer is type char*, but not type string. ie:

{below code is skeleton, some nonrelevant aspects oimitted}

char* buf ; // this works
buf = new char [21] ;
ifstream ifs ;
...........
(void) ifs.getline (buf, 20) ;

using namespace std ; // This gives a compile error on the getline
string buf ;
buf.resize(21) ;
ifstream ifs ;
.........
(void) ifs.getline (buf, 20) ;

This isn't a stopper, I can simply do a type conversion, but would prefer ot keep with strings if possible. According to my C++ std library reference, a string arg should be OK, It works under MSVS2015.

Is there something I need to add to make this work under XE-5 builder? TIA
Andrew Law

Posts: 74
Registered: 11/6/02
Re: getline function with string argument
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2016 1:19 AM   in response to: bruce varley in response to: bruce varley
bruce varley wrote:
I can make getline do a file read OK if the buffer is type char*, but not type string. ie:

{below code is skeleton, some nonrelevant aspects oimitted}

char* buf ; // this works
buf = new char [21] ;
ifstream ifs ;
...........
(void) ifs.getline (buf, 20) ;

using namespace std ; // This gives a compile error on the getline
string buf ;
buf.resize(21) ;
ifstream ifs ;
.........
(void) ifs.getline (buf, 20) ;

This isn't a stopper, I can simply do a type conversion, but would prefer ot keep with strings if possible. According to my C++ std library reference, a string arg should be OK, It works under MSVS2015.

This works in our code base across 2010 <--> 10.1 inclusive - where the ifstream is passed as an argument to the call to std::getline, as opposed to your usage:

		std::ifstream infile(filename.c_str() );
 
		if (infile.is_open() )
		{
			while (infile.good() )
			{
				std::string line;
				std::getline(infile, line);
				// other stuff here
			}
			infile.close();
		}
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: getline function with string argument  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 25, 2016 10:41 AM   in response to: bruce varley in response to: bruce varley
bruce wrote:

I can make getline do a file read OK if the buffer is type char*,
but not type string.

std::ifstream does not have a getline() method that outputs a std::string.
You need to use the std::getline() function instead, eg:

std::string buf;
std::ifstream ifs;
...
std::getline(ifs, buf);


According to my C++ std library reference, a string arg should be OK

Then your reference is wrong, because passing a std::string to std::ifstream::getline()
is not OK:

http://en.cppreference.com/w/cpp/io/basic_istream/getline
http://en.cppreference.com/w/cpp/string/basic_string/getline

http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/string/string/getline/

It works under MSVS2015.

I seriously doubt that.

https://msdn.microsoft.com/en-us/library/ykfk8bc0.aspx
https://msdn.microsoft.com/en-us/library/3ah895zy.aspx

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02