Getline

Hello guys, new in town,

Getline will write in a stream but it will also chop any NULL or EOF at the end. So is there a way to know the number of characters read/written after the execution of the getline? Like a gcount()type. Thanks in advance.

Best regards,

MAB
Have you tried the reference on this site?
You can handle it along these lines in C++:
1
2
3
4
5
6
7
string s;
getline(cin,s);
//type the string which is stored in s
cout<<s;//print the string
int len=0;
len=s.length();//len== length of s including spaces
cout<<len;//print the string length 

@buffbill: How can you tell how many newline characters were dropped by means of the getline() operation? I believe that is the question, not the resulting string's length.
std::getline() will only remove one newline character. However if EOF is reached before a newline then nothing will get removed. So I suppose you could check std::istream::rdbuf()->in_avail() to see if a newline was removed or not.

Alternatively you could use std::istream::getline() which sets gcount() to let you know how many characters were read.
Here is the deal.

I am reading from one single line in a text file with getline. The line consists of words separated but one space to be replaced by a single dash for each empty space except on the last word. Everything good except getline doesn't leave any EOF nor EOL at the end to stop the loop. Note that I am reading straight into a stream var from which I process what I want.

Regards,

MAB
buffbill (299) Jul 26, 2011 at 9:42pm
You can handle it along these lines in C++:


string s;
getline(cin,s);
//type the string which is stored in s
cout<<s;//print the string
int len=0;
len=s.length();//len== length of s including spaces
cout<<len;//print the string length

THAT DID IT. SUCCESS! CASE CLOSED.

Thanks to all that replied!

MAB
If you read the documentation there are states and classes to open a file for certain control. For example if you want to see all characters, or bytes, you need to set up buffers and Use fstream.getline function rather than the global getline, which sets up a default version of the previous. You need to open the file for binary read, rather than delimited reads which are default the carriage return and line feed depending on which system you are starting from.

Again its back to the documentation and search the web, how to use fstream is out there in many simple to understand forms, which is the way we did it before the global getline function.
Topic archived. No new replies allowed.