• Welcome! The TrekBBS is the number one place to chat about Star Trek with like-minded fans.
    If you are not already a member then please register an account and join in the discussion!

How do i right array of structure using c++

TEH BABA

Commodore
Commodore
InventoryFileSystem.open("Inventory.dat", ios::out | ios::binary);

while(intWritingIndex < 10)
{
// Move back to the beginning of this record's position.
InventoryFileSystem.seekp(intWritingIndex * sizeof(record), ios::beg);

// Write the new record over the current record.
InventoryFileSystem.write(reinterpret_cast<char *>(&record),
sizeof(record));

intWritingIndex++;
}

InventoryFileSystem.close();
 
That will only work if record is of a plain-old-data type with no pointer members; and even then the resulting file might not be binary-compatible with the same program built on other compilers or with different compile settings. For a more robust approach you'll need to write functions to serialize and deserialize the structure. Often this is done in an overload of operator >> and operator <<.
 
Last edited:
If you are not already a member then please register an account and join in the discussion!

Sign up / Register


Back
Top