Click to See Complete Forum and Search --> : Question about istringstream.getline method.


George2
April 15th, 2003, 07:57 AM
Hi, everyone!

After invoking getline, I want to know what is appended at
the end of "output" in my case. It seems it is not \0. But
"strlen" function returns OK.

Output is:

--------
12
2
50
3456789
7
57
123456789
9
57
--------

Source codes:

--------
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
char input[] = "12\n3456789\n123456789";
char output[30];

istringstream is(input);
while (is.getline (output, sizeof (output), '\n'))
{
cout << output << endl;
cout << strlen (output) << endl;
if (0 == output [strlen (output) - 1])
{
cout << "Null is appended! " << endl;
}
cout << unsigned int (output [strlen (output) - 1]) << endl;
}

return 1;
}
--------


Thanks in advance,
George

Philip Nicoletti
April 15th, 2003, 08:20 AM
The strlen() does not include the NULL character, your
check should be :


if (0 == output [strlen (output)])
{
cout << "Null is appended! " << endl;
}

George2
April 15th, 2003, 08:24 AM
Thanks, Philip buddie!

I really made a mistake. //shy

George

Originally posted by Philip Nicoletti
The strlen() does not include the NULL character, your
check should be :


if (0 == output [strlen (output)])
{
cout << "Null is appended! " << endl;
}

George2
April 15th, 2003, 08:25 AM
Originally posted by Philip Nicoletti
The strlen() does not include the NULL character, your
check should be :


if (0 == output [strlen (output)])
{
cout << "Null is appended! " << endl;
}

Philip Nicoletti
April 15th, 2003, 08:38 AM
I prefer using std::string as input rather than char *.
In your code, check what happens if you change
the one line to :


char input[] = "1234567890123456789012345678901234567\n12\n";


using std::string

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
char input[] = "12\n3456789\n123456789";
string output;

istringstream is(input);
while (std::getline (is,output))
{
cout << output << endl;
cout << strlen (output.c_str()) << endl;
cout << unsigned int (output [strlen (output.c_str()) - 1]) << endl;
}

return 1;
}

George2
April 15th, 2003, 09:01 AM
Thanks, Philip buddie!

I have tried your sample code and it works ok.
I have tried another sample, but it has odd behavior.

Can you help?

Please look at the sample codes below:

Output:
--------
12
2
Null is appended!
0
--------

Source code:

--------
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
char input[] = "12\n3456789\n123456789";
char output [3];

istringstream is (input);
while (is.getline (output, sizeof (output), '\n'))
{
cout << output << endl;
cout << strlen (output) << endl;
if (0 == output [strlen (output)])
{
cout << "Null is appended! " << endl;
}
cout << unsigned int (output [strlen (output)]) << endl;
}

return 1;
}
--------


But when I change "char output [3];" to "char output [0xff];", the output
is OK! Tt is,

--------
12
2
Null is appended!
0
3456789
7
Null is appended!
0
123456789
9
Null is appended!
0
--------

I have made a reference to MSDN and failed to find any answers.
Can you help me to explanation the reason?


regards,
George

Originally posted by Philip Nicoletti
I prefer using std::string as input rather than char *.
In your code, check what happens if you change
the one line to :


char input[] = "1234567890123456789012345678901234567\n12\n";


using std::string

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
char input[] = "12\n3456789\n123456789";
string output;

istringstream is(input);
while (std::getline (is,output))
{
cout << output << endl;
cout << strlen (output.c_str()) << endl;
cout << unsigned int (output [strlen (output.c_str()) - 1]) << endl;
}

return 1;
}

Philip Nicoletti
April 15th, 2003, 09:21 AM
The reason is the same as I alluded to in my last
post concerning using std::string instead of char*.

using :


while (is.getline (output, sizeof (output), '\n'))


if the number of characters in the line is more than
sizeof(output)-1, then the stream's failbit is set, which
kicks you out of the loop. When you use char*, you need
to know the maximum line size. With std::string you do not.

George2
April 15th, 2003, 09:27 AM
Oh, I see ...

Thanks, Philip buddie!


George

Originally posted by Philip Nicoletti
The reason is the same as I alluded to in my last
post concerning using std::string instead of char*.

using :


while (is.getline (output, sizeof (output), '\n'))


if the number of characters in the line is more than
sizeof(output)-1, then the stream's failbit is set, which
kicks you out of the loop. When you use char*, you need
to know the maximum line size. With std::string you do not.