fgets and newline

M

Mike Mimic

Hi!

Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
markers as line separators?

Is there some other library function which would read line by line (end
line markers do not need to be included).


Mike
 
J

Jacques Labuschagne

Mike said:
Hi!

Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
markers as line separators?

Is there some other library function which would read line by line (end
line markers do not need to be included).


Mike

std::getline?


Jacques.
 
M

Mike Mimic

Hi!

Jacques said:
std::getline?

I missed that.

But if line is terminated with \r\n that I will have to call getline
twice for every line as I will get empty line after \r.

And how can I convert file descriptor to a C++ stream (as fdopen)?


Mike
 
J

Jacques Labuschagne

Mike said:
I missed that.

But if line is terminated with \r\n that I will have to call getline
twice for every line as I will get empty line after \r.

Not if you use \n as your delimiter and just erase the \r by hand.
Something like:

getline(myfile, mystring, '\n');
if (*mystring.rbegin() == '\r'){
*mystring.rbegin() = ' ';
// or you could actually call mystring.erase()
}
And how can I convert file descriptor to a C++ stream (as fdopen)?

fdopen() is not part of standard C or C++. If you're doing platform
specific stuff you may need to write your own getline() equivalent.


Jacques
 
J

John Harrison

Mike Mimic said:
Hi!

Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
markers as line separators?

No, write your own function to do this. Simple enough I think.
Is there some other library function which would read line by line (end
line markers do not need to be included).

std::getline, but again doesn't do the processing on end of lines you
require.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top