473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

The getline function

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
Jul 22 '05 #1
4 2624
"Tcc" <as**@yahoo.com> wrote in message
news:ch***********@news.hgc.com.hk...
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


Jul 22 '05 #2

"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
Jul 22 '05 #3
> 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
Jul 22 '05 #4

"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;
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
10
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
22
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
2
by: FightingWolf | last post by:
Hey, first of all sorry for my bad english, but I'm from Germany ;) I found something special in the getline() function, I don't understand. If I use the getline function in case of an...
1
by: hauser | last post by:
Hey, first of all sorry for my bad english, but I'm from Germany I found something special in the getline() function, I don't understand. If I use the getline function in case of an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.