Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-04
7
Medium Priority
?
394 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

636 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