?
Solved

Wrting  int, double, float and char datatypes at the same type.

Posted on 2004-08-04
7
Medium Priority
?
388 Views
Last Modified: 2010-04-01
g'day experts,

I ave a need to write write the folowing data to file. they are in format:

ID- char[20]
name-char[20]
current - unsigned int
usuage - unsigned int
code- char[20]
cost - float.

I a writing to file in the format;

outCustBill.write((char*)(&SaveBill),sizeof(SaveBill));

how do I save all the different datatypes together. Thank you.
0
Comment
Question by:claracruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 11713351
You can create a struct with these data:

typedef struct Data {
    char[20] ID;
    char[20] name;
    unsigned int current;
    unsigned int usage;
    char[20] code;
    float cost;
} Data_;

Data myData; // and fill the vars.

outCustBill.write (&myData, sizeof (myData));
0
 
LVL 4

Author Comment

by:claracruz
ID: 11715867
please explain the use of struct.... thank you
0
 
LVL 13

Expert Comment

by:SteH
ID: 11716019
One error correction first: it seems that write needs a char* so cast the first parameter to it
outCustBill.write ((char*) &myData, sizeof (myData));

a struct is a special case class which puts different typed variables into a a single object. Special in the sense that it has public members by default. In this case you collect all the different variables into a single object which can now be written in one go to file (and read back as well). This is also usefull if you need to return different types of variables from a function. An array is not possible since types differ and having all as parameters can harm the legibility of the code.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Author Comment

by:claracruz
ID: 11716297
thank you.

also when you say Data myData; // and fill the vars.

 what exactly does this mean?
0
 
LVL 13

Expert Comment

by:SteH
ID: 11716390
The line
Data myData;
creates an object of type Data but all its members have random values yet.

Lines like
strcpy (myData.name, "name1");
myData.current = 13;
myData.cost = 10.5;
do that part and are needed before writing myData to file.
0
 
LVL 4

Author Comment

by:claracruz
ID: 11717283
Hi there,

Thanks for all the help... still one more thing though,

I have implemented this and now get error

      ......customer.h(458) : error C2664: 'write' : cannot convert parameter 1 from 'struct Data *' to 'const char *'on line;

              outCustBill.write (&myData, sizeof (myData));

Would you have any ideaof the possible cause of the error.

Thanks
0
 
LVL 13

Accepted Solution

by:
SteH earned 2000 total points
ID: 11723722
Try
outCustBill.write ((char*) &myData, sizeof (myData));
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

  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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 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