Solved

Saving structures to files

Posted on 2000-04-11
5
187 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now