The getline function

T

Tcc

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
 
I

Ivan Vecerina

Tcc said:
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
 
J

John Harrison

Tcc said:
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
 
J

John Harrison

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
 
M

Mike Wahler

Tcc said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top