# C++: Getting input from ifstream

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a class that has a function called SetXPosition(float x) which will either set the X position of the class or if there is already one set it will delete it and create a new one with the value passed. Now I am trying to load input from a .txt file.
ifstream inputFile;

Now I realise input can be received as such inputFile >> var; But I am wondering if there is a way to do something like this: SetXPosition(inputFile.getdata); Or something like that, now I realise that isn't an actual function thats just an example. Any help is appreciated :)

##### Share on other sites
template <typename T>T extract(std::istream & istr) {  T temp;  istr >> temp;  return temp;}SetXPosition(extract<float>(inputFile));

##### Share on other sites
Quote:
 Original post by KieranWI have a class that has a function called SetXPosition(float x) which will either set the X position of the class or if there is already one set it will delete it and create a new one with the value passed.

That x position is a float, right? If so, why are you deleting and recreating it whenever you want to change it?

##### Share on other sites
Thanks SiCrane, thats a nice piece of code.

Is it possible to do:

int* myVar = new int(10);
myVar = 12;

Without causing a memory leak?
I thought you have to free the memory 10 is taking and then create 12 again on the heap?

EDIT: umm I meant *myVar = 12;

[Edited by - KieranW on January 26, 2009 10:57:52 PM]

##### Share on other sites
Quote:
 Original post by KieranWThanks SiCrane, thats a nice piece of code.Is it possible to do:int* myVar = new int(10);myVar = 12;Without causing a memory leak?I thought you have to free the memory 10 is taking and then create 12 again on the heap?
It would actually be:
int* myVar = new int(10);*myVar = 12;
(Note the dereference in the second line.) And yes, if you don't delete myVar at some point, you'll have a memory leak.

However, in C++ we generally avoid using pointers and dynamic allocation unless we have a reason to do otherwise. For example, instead of the above code, we would write:
int myVar = 10;myVar = 12;
Before using dynamic allocation in C++, be sure to ask yourself what purpose it serves, and whether it's appropriate and/or necessary in the given context.

[Edit: Re-reading your post, it looks like you just have a misconception about how memory is managed in C++. No, you don't have to free the memory associated with a variable before you can assign a new value to it; you simply assign a new value to it, at which point the old value is overwritten.]

##### Share on other sites
You have to dereference the pointer first, but yes it is possible without allocating more memory.

Not sure where you are with pointers, but as myVar is a pointer, it actually holds a memory location and not an actual 'integer', or atleast not the one you expect.

Try printing 'myVar' using std::cout or similar to see what I mean.

What you need to do is 'cancel out' the pointer by placing another * infront.

I.e,
*myVar = 12

myVar = 12

Will do what you want. Read it as "whatever is pointed at by the integer pointer myVar".

Edit:
Damn, bet me to it jyk.

##### Share on other sites
I'm sorry for the confusion, yes I did mean *myVar = 12, just forgot the *.
Also I was thinking about arrays as opposed to variables as they have to be deleted to resize them, don't know how I associated that with variables though. Anyway, the reason I was asking mainly was because I was forgetting it allocates enough memory for an Integer not just the number it is passed.

Yes I have learnt pointers and memory allocation, I actually learnt it through a book, I just forget a few things every now and again. Yes I realise I have to delete it at some point which is why I do so in the constructor.

Quote:
 [Edit: Re-reading your post, it looks like you just have a misconception about how memory is managed in C++. No, you don't have to free the memory associated with a variable before you can assign a new value to it; you simply assign a new value to it, at which point the old value is overwritten.]

Yes this is the answer I was after here, thankyou. :)
By the way, how do you get the code in those boxes? [ code ] [ / code ] only changes the font, doesn't use a box.

##### Share on other sites
Quote:
 Original post by KieranWBy the way, how do you get the code in those boxes? [ code ] [ / code ] only changes the font, doesn't use a box.

[ source ][ /source ] without spaces.

You can click the "edit" button on another user's post to see how they formatted their post if you are ever curious about how something was done. Obviously you can't submit any changes to anyone else's post, but you will then see the post in its raw format.