473,890 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2655
"Tcc" <as**@yahoo.com > wrote in message
news:ch******** ***@news.hgc.co m.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.co m.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_iter ator<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.co m.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
3563
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 function at first and then I use the cin>> function then the next time cin.getline function does not work.
5
7754
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 that shows how to use the 'getline' function. It said: "The getline function allows you to specify how many bytes you will get from the users input. Each character the user's types takes up one byte. So if, in the getline function, you specify...
1
2153
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 could state getline's first parameter supports "FROM". The second parameter supports "TO". // later
10
5622
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 parameter 1 from 'const char *' to 'char *' Conversion loses qualifiers I have a data file called Standard.udf The Data File (not the problem): The data in the file is in text format, and was created using a function in this program with...
14
3901
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 ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy enough, but I can't get the getline(file_name array_name) to work. So...I am thinking it is definitely...
18
8270
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 <iostream> #include <fstream> #include <cstdlib> #include <string>
22
3070
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 structure. The compiler obviously allows VLA however it craps out after the above amount of bytes. I was told i was attempting to put everythng on the stack and not the heap. So i was wondering if anyone can maybe clear it up, is that true? would i...
33
25240
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
1437
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 Excel-sheet or an image - to get the image-data - the getline function breakes after a NULL termination ( \0 ) fstream oFile_e;
1
1539
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 Excel-sheet or an image - to get the image-data - the getline function breakes after a NULL termination ( \0 )
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5851
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6045
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.