Hi, im at the end of my tether after spending hours trying to find a solution to this so hopefully somebody here can help me out.

I have a structure, customer, which contains just a few details such as name address phone number etc.

I need to store this info in a text file(not binary as i can do that and it is not a project requirement) to create.

Is there a way to output a whole struct to a text file in one go, instead of variable by variable which is all i can do at the moment?

Likewise can I pull the information back out of the text file in structure form or does it have to be done character by character?

Any help would be great!
Thanks

>>Is there a way to output a whole struct to a text file in one go, instead of variable by variable which is all i can do at the moment?
In a text file that's the only way to do it. You can put all the variable for one structure on one line, with spaces or some other separator between each variable, and a '\n' at the end of the line, or you can put each veriable on its own line. Which way to do it is probably up to you (assuming your instructions do not state otherwise)

>>Likewise can I pull the information back out of the text file in structure form or does it have to be done character by character?
Not necessary to do character-by-character. Exactly how to do it depends on (1) how the file is written and (2) the structure itself. You didn't post the structure so I can't say further. But if there are no embedded spaces in the variable strings then you could use ifstream's the >> operator to read the entire variable value.

Thanks,
I dont have the code on the machine im working on now but basically its just 6 variables to store char[]s name,address, favourite movie etc... and 2 variables for phone numbers.

So am I correct to say that I can use seperate lines for each instance of the structure and a space or some other symbol between each variable on each line.

Can I then use this to easily parse thorugh the seperate lines and variables to recreated the structs when recieving output from the text file?

Thanks very much, im just making sure i have the most efficient way of doing this.

Im working on something similar. Im able to get every thing out of the text file and into a vector using

vector<string> v;
ifstream in("textfile.txt");
string line;
while(getline(in, line))
v.push_back(line);

Does anybody have ideas on how to change this vector(unknown size) over to create an array of structs.

Thanks for any suggestions in advance. Tom.

Overload the << operator for the struct/class to output all the information you want in one operation using whatever format you want.

//declare the struct
struct mine
{//whatever};

//declare an array of 10 structs of type mine
mine myStructArray[10];

If you don't know the number of items to go into the array then you'll need to declare an array of an arbitrarily large size and keep track of how many items you have entered into it so you don't overwrite the array, or use a different container, like a list, that isn't so size sensitive.

This article has been dead for over six months. Start a new discussion instead.