"Tcc" <as**@yahoo.com> wrote in message
news:ch***********@news.hgc.com.hk...
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