0

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!

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

No I don't think he wants us to use any extra librarys. I plan on doing this by myself, I just need to understand how to read & write structs or XML's to a file !!! lol
except a raw dump of the structure, it will be easier to use a lib of some kind than to roll your own.
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.
Alright I will see what I can do with them. I just wish someone gave me an example on how to use them!
2

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);

7 Comments

I REALLY like your idea. Could you provide a short example of how to do this?
@BCS: ANSI/ISO C's IO is 'fopen', 'fread', 'fwrite'. 'open', 'read', 'write' are POSIX/K&R C. Oh, and 'open' looks like 'open("newfile.dat", O_CREAT | O_BINARY)' or 'open("log.txt", O_RDONLY | O_TEXT)'.
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 ?
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.
@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.
|
1

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

4 Comments

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

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

1 Comment

How do I do that.... can you give me an example on how I would read and right that to a file ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.