Solved

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

Posted on 2004-08-04
7
345 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
  • 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 Security Threats Are You Missing?

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.

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 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

18 Experts available now in Live!

Get 1:1 Help Now