views:

261

answers:

4

I'm working on a homework project and i'm trying to store inventory data into a file.

The inventory data size shouldn't be too large cause technically no one is going to really use it.

I need to write these contents to a file:

• Item Description

• Quantity on Hand

• Wholesale Cost

• Retail Cost

• Date Added to Inventory

I am going to need to make an interface that allows me to do this:

• Add new records to the file

• Display any record in the file

• Change any record in the file

Struct would be the easiest way to go about this imo. If I can just figure out how to read / write structs to a file this should be really easy.

If you could provide a small example on how to do this I would really appreciate it.

Thanks!

A: 

I would go with XML; it's structured, it's text based so you can look at it with any text editor.

Max
How do I do that.... can you give me an example on how I would read and right that to a file ?
OneShot
+1  A: 

IOStream library does it

The ofstream class provides the interface to write data to files as output streams.

The ifstream class provides the interface to read data from files as input streams

Edit- Example

TStamper
I see how you could write a structure to a file with "write" using ofstream. But how would you read it back in ?
OneShot
with the ifstream...you would first use the ifstream to read a file, it can be blank..then write to the same file
TStamper
Can you show me a example on how to do this? Just a small one ?
OneShot
updated with example
TStamper
Good link, that helped me a lot. Now I just have to see if I can get this example you provided to work.
OneShot
+2  A: 

Ask your teacher, could you use boost library.
If yes, read boost serilization tutorial, it contains a simple examples: http://www.boost.org/doc/libs/1_38_0/libs/serialization/doc/tutorial.html

But if you want understand how to work with files, you should do this works without any help or boost.

If you want works with std::[io]fstreams you should decide what format you will support:
- text - for this case best way define operator<< and operator>> and use them for writing structure to file or reading;
- binary - your structure should be POD ( plain old data ) and doesn't should contain pointers - and you will use read and write streams methods.
example for binary file:
http://www.codeguru.com/forum/showthread.php?t=269648

bb
OneShot
except a raw dump of the structure, it will be easier to use a lib of some kind than to roll your own.
BCS
In life usualy it easy and better using well deigned third party lib instead investigating bicycle. But If you want understand low level - you sohuld use std::[io]fstreams.
bb
Alright I will see what I can do with them. I just wish someone gave me an example on how to use them!
OneShot
+2  A: 

If you don't mind really low level, you can just bit copy the structs in and out by casting a pointer to the struct to void* and using sizeof() to get the struct length. (IIRC their is a way to dump/read a void buffer to/from a file)


Note this ONLY works if the data has no pointers/references/etc.


I like C's IO better than C++'s so:

typedef struct { int hi; int mon; char[35] dat; } S;

S s;
S arr[22];
int f;


  // write
f = open(/* I forget the args*/);

  // one
if(sizeof(s) != write(f, &s, sizeof(s))) Error();
  // many
if(sizeof(arr) != write(f, arr, sizeof(arr))) Error();

close(f);


  // read
f = open(/* I forget the args*/);

  // one
if(sizeof(s) != read(f, &s, sizeof(s))) Error();
  // many
if(sizeof(arr) != read(f, arr, sizeof(arr))) Error();

close(f);
BCS
I REALLY like your idea. Could you provide a short example of how to do this?
OneShot
Simon Buchan
So it is that easy? What if the integer value is 250259 instead of 2509 wouldn't the size of "s" change making the read in not work ?
OneShot
I know that the open/read/write it not the streamIO stuff. If all you are doing is reading binary blobs then the POSIX stuff is enough for me.
BCS
@OneShot: int is 32 bits long no mater what (unless it's 16 or 64 bits but that depends on the compiler). OTOH don't expect to read the resultant file with a text editor.
BCS
How do I know when to stop, and read the next Struct? THis file will save multiple structs to the file.
OneShot
@OneShot; You will need to figure out what order the structs are in during reading your self but running more than one read/write in a row will work and end-of-file can be detected by the given `if` statment.
BCS