Jump to content
  • Advertisement
Sign in to follow this  
KieranW

C++: Getting input from ifstream

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

If you intended to correct an error in the post then please contact us.

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;
inputFile.load("name.txt");
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 this post


Link to post
Share on other sites
Advertisement

template <typename T>
T extract(std::istream & istr) {
T temp;
istr >> temp;
return temp;
}

SetXPosition(extract<float>(inputFile));

Share this post


Link to post
Share on other sites
Quote:
Original post by KieranW
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.

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by KieranW
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?
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 this post


Link to post
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

Instead of,
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by KieranW
By 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!