Solved

Saving structures to files

Posted on 2000-04-11
5
186 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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