Jump to content
  • Advertisement
Sign in to follow this  
Shruubi

writing an objects name to file

This topic is 2563 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, for the purpose of this question it is a simple class called person which stores their age and height (in cm). Now say i wanted to write the objects of this class to a file, the name of the object would be the persons name, and the class contents would obviously be the relevant persons age and height. How would i go about writing all this information to a file (including the persons name), for recall without defining the persons name as a string within the class?


#include <iostream>
#include <fstream>

using namespace std;

class person
{
public:
int age;
int height;
}

int main() {
person bob;
ofstream myfile;
myfile.open("file.txt", ios::app);
bob.age = 18;
bob.height = 180;
myfile << /*write the object name somehow*/"" << bob.age << " " << bob.height << "\n";
myfile.close();
return 0;
}


i hope my question has made sense.

Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
You can't. Objects don't have names in C++. In your code, you can use "bob" as a string constant, but I doubt that's what you wanted.

Share this post


Link to post
Share on other sites
so there is no way to be able to write to a file: bob 18 180?

i guess this would then apply to any variable, so while it is possible to write the variable value to a file, you cannot write the variable name?

Share this post


Link to post
Share on other sites
You can write the name as a string literal.
myfile << "bob " << bob.age << " " << bob.height << "\n";

If person has a name in your program you should just store it as a member of person

Share this post


Link to post
Share on other sites
no, it was clear, i guess i was just hoping for a different answer, but obviously one does not exist. thank you for your help.

Share this post


Link to post
Share on other sites
You can using a macro like:


#define NAME_OF(x) #x
....
myfile << NAME_OF(bob) << bob.age;


with this macro you can get the string of any expression.

Share this post


Link to post
Share on other sites
Just give the person a class a "string name" field? I would think that would be the proper way to handle it.


class person
{
public:
string name;
int age;
int height;
}

int main()
{
person bob;
ofstream myfile;
myfile.open("file.txt", ios::app);
bob.name = "Bob";
bob.age = 18;
bob.height = 180;
myfile << bob.name << bob.age << " " << bob.height << "\n";
myfile.close();
return 0;
}

Share this post


Link to post
Share on other sites
What RedEyedKiller says works but it is just overcomplicated to be useful. It is so much easier to write "bob" than write NAME_OF(bob) when both do the exact same thing.

Share this post


Link to post
Share on other sites
This is what I do in my Logging system (I hope it's useful :) )


//the macro
#define ECHO_EXPR(x) #x " = " << x << " "

//overload << operator to make logging easier
std::ostream& operator << (std::ostream& stream,const ClassA& arg)
{
stream << arg.GetMemberA() << " " << arg.GetMemberB();
return stream;
}

//the actuall use
file << ECHO_EXPR(var1);


edit:sorry for the typo Edited by RedEyedKiller

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!