Solved

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

Posted on 2004-08-04
7
369 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
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.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

828 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