Sign in to follow this  

writing an objects name to file

This topic is 2343 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?

[code]
#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;
}
[/code]

i hope my question has made sense.

Thanks in advance

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.
[code]myfile << "bob " << bob.age << " " << bob.height << "\n";[/code]

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
Just give the person a class a "string name" field? I would think that would be the proper way to handle it.

[code]
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;
}[/code]

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 :) )

[code]
//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);
[/code]

edit:sorry for the typo Edited by RedEyedKiller

Share this post


Link to post
Share on other sites
[quote name='RedEyedKiller' timestamp='1310656897' post='4835288']
[code]
#define ECHO_EXP(x) #x " = " << x << " "
[...]
file << ECHO_XPR(var1);
[/code]
[/quote]

Sorry for the off-topic comment, but abbreviations should almost always be avoided: They are not as obvious as they seemed when you wrote them. RedEyeKiller couldn't keep his abbreviations consistent in a 10-line piece of code! :)

Besides: EXP=EXPONENT? XPR=CROSSING_PRICE? XPR=PROJECTION_ONTO_THE_X_AXIS?

Share this post


Link to post
Share on other sites

This topic is 2343 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this