CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Question about istringstream.getline method.

    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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    The strlen() does not include the NULL character, your
    check should be :

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

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking

    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 :

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

  4. #4
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    Originally posted by Philip Nicoletti
    The strlen() does not include the NULL character, your
    check should be :

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

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    I prefer using std::string as input rather than char *.
    In your code, check what happens if you change
    the one line to :

    Code:
    char input[] = "1234567890123456789012345678901234567\n12\n";
    using std::string
    Code:
    #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;
    }

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    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 :

    Code:
    char input[] = "1234567890123456789012345678901234567\n12\n";
    using std::string
    Code:
    #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;
    }

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    The reason is the same as I alluded to in my last
    post concerning using std::string instead of char*.

    using :

    Code:
    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.

  8. #8
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    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 :

    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured