Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > The getline function

Reply
Thread Tools

The getline function

 
 
Tcc
Guest
Posts: n/a
 
      09-06-2004
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


 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-06-2004
"Tcc" <(E-Mail Removed)> wrote in message
news:chhjhv$1s9r$(E-Mail Removed)...
> 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




 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-06-2004

"Tcc" <(E-Mail Removed)> wrote in message
news:chhjhv$1s9r$(E-Mail Removed)...
> 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-06-2004
> 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-07-2004

"Tcc" <(E-Mail Removed)> wrote in message
news:chhjhv$1s9r$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use lib function "getline" Chen shuSheng C Programming 33 08-28-2006 05:38 AM
How to use lib function "getline" Chen shuSheng C++ 4 08-21-2006 04:05 AM
Help! SIGBUS (object specifc hardware error) when call function getline wqyuwss@hotmail.com C++ 4 03-02-2006 06:46 AM
getline function vknid C++ 5 07-31-2004 01:38 AM
Re: getline function John Harrison C++ 0 08-29-2003 01:37 PM



Advertisments