The getline function

Discussion in 'C++' started by Tcc, Sep 6, 2004.

  1. Tcc

    Tcc Guest

    Hi all, I have a question about using the function getline()..

    e.g. I have defined a structure:

    struct s {
    string *a;
    Queue *b;
    };

    then in my main function:

    main () {
    s * t;

    ifstream in("aaa.txt"); // an input file
    getline(in, *t->a);<-------------------------------------this one

    In this case, I would like to store the content of aaa.txt in to the struct
    string *a, is it possiblew to do this?


    Thanks
     
    Tcc, Sep 6, 2004
    #1
    1. Advertising

  2. "Tcc" <> wrote in message
    news:chhjhv$1s9r$...
    > e.g. I have defined a structure:
    >
    > struct s {
    > string *a;
    > Queue *b;
    > };

    Why do you store (raw) pointers in your structure?
    In C++, this is likely cause memory management headaches.

    You may want to use the following instead:
    struct s {
    string a;
    Queue b;
    };


    > then in my main function:
    >
    > main () {
    > s * t;

    Again, this should be:
    s t;
    >
    > ifstream in("aaa.txt"); // an input file
    > getline(in, *t->a);<-------------------------------------this one

    This would lead to undefined behavior, because no memory has
    been allocated for *t or *(t->a).

    > In this case, I would like to store the content of aaa.txt in to the
    > struct
    > string *a, is it possiblew to do this?

    If you make the modifications I suggested above,
    you can simply write:
    getline( in, t.a );


    I hope this helps,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Sep 6, 2004
    #2
    1. Advertising

  3. "Tcc" <> wrote in message
    news:chhjhv$1s9r$...
    > Hi all, I have a question about using the function getline()..
    >
    > e.g. I have defined a structure:
    >
    > struct s {
    > string *a;
    > Queue *b;
    > };
    >
    > then in my main function:
    >
    > main () {
    > s * t;
    >
    > ifstream in("aaa.txt"); // an input file
    > getline(in, *t->a);<-------------------------------------this one
    >
    > In this case, I would like to store the content of aaa.txt in to the

    struct
    > string *a, is it possiblew to do this?
    >
    >
    > Thanks
    >


    getline reads lines of text, you want to read a whole file.

    Also you are using uninitialised pointers. Probably you should be using
    pointers at all, but if you must you should at least initialise them.

    Here's a pointerless piece of code that reads a whole file into a string
    (untested code).

    #include <algorithm>
    #include <fstream>
    #include <iterator>
    using namespace std;

    struct s {
    string a;
    Queue b;
    };

    int main () {
    s t;
    ifstream in("aaa.txt"); // an input file
    copy(istreambuf_iterator<char>(in), istreambuf_iterator<char>(),
    back_inserter(t.a));
    }

    Of course that isn't the only way to do it. Another way would be simply to
    read one char at a time from your file and append each char to the string. A
    simple while loop, something like this.

    char ch;
    while (in.get(ch))
    {
    t.a += ch;
    }

    But however you do it you've got to fix the issues with pointers that you
    have at the moment.

    john
     
    John Harrison, Sep 6, 2004
    #3
  4. > Also you are using uninitialised pointers. Probably you should be using
    > pointers at all, but if you must you should at least initialise them.
    >


    should NOT be using pointers.

    john
     
    John Harrison, Sep 6, 2004
    #4
  5. Tcc

    Mike Wahler Guest

    "Tcc" <> wrote in message
    news:chhjhv$1s9r$...
    > Hi all, I have a question about using the function getline()..
    >
    > e.g. I have defined a structure:
    >
    > struct s {
    > string *a;
    > Queue *b;
    > };
    >
    > then in my main function:
    >
    > main () {
    > s * t;


    t = new s;
    s->a = new std::string;

    >
    > ifstream in("aaa.txt"); // an input file
    > getline(in, *t->a);<-------------------------------------this one
    >
    > In this case, I would like to store the content of aaa.txt in to the

    struct
    > string *a, is it possiblew to do this?


    No. You cannot store a string in a pointer. Pointers are
    for storing addresses (or the null pointer).

    You also invoked undefined behavior by dereferencing
    the uninitialized pointer 't'.

    Is there some reason you feel you must use pointers for this?

    -Mike
     
    Mike Wahler, Sep 7, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: getline function

    John Harrison, Aug 29, 2003, in forum: C++
    Replies:
    0
    Views:
    364
    John Harrison
    Aug 29, 2003
  2. vknid

    getline function

    vknid, Jul 28, 2004, in forum: C++
    Replies:
    5
    Views:
    2,577
    Owen Jacobson
    Jul 31, 2004
  3. Chen shuSheng

    How to use lib function "getline"

    Chen shuSheng, Aug 20, 2006, in forum: C Programming
    Replies:
    33
    Views:
    2,970
    Dave Thompson
    Aug 28, 2006
  4. Replies:
    4
    Views:
    692
  5. Chen shuSheng

    How to use lib function "getline"

    Chen shuSheng, Aug 20, 2006, in forum: C++
    Replies:
    4
    Views:
    324
    red floyd
    Aug 21, 2006
Loading...

Share This Page