Solved

Saving structures to files

Posted on 2000-04-11
5
191 Views
Last Modified: 2010-04-02
Helloo..

I'm doing a project and i have assigned various values to structure members in my main program.. i wanted to know how i could transfer the data values from the structure into a text file for read and write operations.

In the main program the data is read in to the structure using an array.. so i want to pass the value each time to the seperate file before the end of the loop is reached...

I hope this makes sense and you can help!!!
0
Comment
Question by:anita177
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2703213
it woudl help if youy posted the code....

since you want the structure in text, not binary, format, you will have to write out each data member individually and converted to an ASCII format.

You can use a fstream object to help you do this as it will do the conversion to ASCII and write the information in onse step.

example follows
0
 
LVL 22

Expert Comment

by:nietod
ID: 2703227
since I don't know what information you have to write out, I'll make up a structure like

struct Data
{
    int i;
    double d;
    char c[10];
};


you would write the file like

fstream Fil("C:\\SOMENAME.TXT",ios::out);
Data D = ????;

Fil << D.i << ' ' << D.d << ' ' << D.c << endl;

Then you could read it like

fstream Fil("C:\SOMEFILE.TXT,ios::in);
Data D;

Fil >> D.i >> D.d >> D.c;

Let me know if you have any questions.   (I'll probably need more details from you though.)
0
 

Author Comment

by:anita177
ID: 2703718
Ok....

I've read the d
0
 
LVL 22

Expert Comment

by:nietod
ID: 2703737
Huh?    Is there a problem?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2703941
Why the low grade??
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question