tags:

views:

397

answers:

13

I have always been able to read and write basic text files in C++, but so far no one has discussed much more than that.

My question is this:

If developing a file type by myself for use by an application I also create, how would I go about writing the data to a file and preserve the layout, formatting, etc.? Are there any standards, or does it just depend on the creativity of the programmer?

A: 

If you create a binary file , you can write any file to it . The only drawback is that you have to know exactly where it starts and where it ends .

Vhaerun
A: 

You usually use a third party library for these things. For example, you would link in a database library for say Oracle that would allow you to talk to the database. Because the underlying file type, ( i.e. Excel spreadsheet vs Openoffice, Oracle vs MySQL, etc. ) differ these libraries abstract away your need to care how the file is constructed.

Hope that helps you find what you're looking for!

Frank Wiles, Revolution Systems, www.revsys.com

Frank Wiles
+2  A: 

There are a number of standards of course. The likely one to use is some flavor of xml since there are libraries and tools that already exist to help you work with it, but nothing is stopping you from inventing your own.

cazlab
+3  A: 

You basically have to come up with your own file format and write binary data. You can also serialize your object model and write the output to a file, but that's usually less efficient.

Better to use an existing database, or use xml (or other) for simple needs. If you want to write a file in a format that already exists, find a library that supports it.

David Thibault
Do not serialize your object model. One change to your object model and you can read old formatted files.
jmucchiello
+1  A: 

Well you could store the data in a format you could read, but which maintained the integrity of your data (XML or JSON for instance).

Or (shudder) you could come up with your own propriatory binary format, and use that.

Marc Gear
+2  A: 

you would go at it exactly the same way as you would a text file. writing your data byte by byte, encoded in such a way that when you read the file you know what you are reading. for a spreadsheet application you could even use a text format (OOXML, OpenDocument) to store presentation and content information.

Or you could define binary datastructures and write that directly to the file.

the choice between text or binary format depends on the application. for a configuration file you may prefer a text file which can be modified outside your app, for a database you will most likely choose a binary format for performance reasons.

Jean
+2  A: 

You have to know the binary file format for the file you are trying to create. Consider Joel's post on this topic: the 97-2003 File Format is a 349 page spec.

Nearly all the time, to do something like that, you use an API, to avoid the grunt work. Be careful however, because trial and error and figuring out "what works" by trial and error can result in an upgrade of the program breaking your code. Plus you have to take into account other operating systems, minor version differences, patches, etc.

Tom Ritter
+1  A: 

See wotsit.org for information on file formats for various file types. Example: You can figure out exactly how to write out a .BMP file and how it is composed.

Writing to a database can be done by using a wrapper class in your language, mainly passing it SQL commands.

Brian R. Bondy
A: 

Use xml (something open, descriptive, and validatable), and stick with the text. There are standards for this sort of thing as well, including ODF

Nerdfest
A: 

You can open the file as binary, instead of text (how one does this depends somewhat on the platform), from there you can write the data directly out to disk. The only real caveat to this is endianess, which can become an issue when moving the files from one architecture to another (x86 to PPC for instance).

Writing binary data to disk is really no harder than writing text, and really, your creativity is key for how you store the data.

foxxtrot
A: 

The general problem is usually referred to as serialization of your application state and in your case with a source/target of a file in whatever format makes sense for you. These days the preferred input/output format is XML, and you may want to look into the existing standards in this field. The problem then becomes how do I map from the state of my system to the particular schema. Boost has a serialization framework that you may want to check out.

/Allan

Allan Wind
A: 

There are a variety of approaches you can take, but in general you'll want some sort of serialization library. BOOST::Serialization, or Google's Protocal Buffers are a good example of these. The basic idea is that you have memory structures (classes and objects) that represent your data, and you want to write that data to a file in a way that can be used to reconstruct those structures again.

If you're hesitant to use a library, you can do it all manually, but realize that you can end up writing a lot of redundant code, or developing your own library. See fopen, fread, fwrite and fclose for a starting point.

Eclipse
A: 

A typical binary file format for custom data is an "indexed file format" consisting of

-------
|index|
-------
|data |
-------

Where the index contains records "pointing" to the data.

The index consists of records containing an offset and a size. The offset tells you where in the file the data is stored and the size tells you the size of the data at that offset (i.e. the number of bytes to read).

typedef struct {
  size_t offset
  size_t size
} Index

typedef struct {
  int  ID
  char First[20]
  char Last[20]
  char *RandomInfo
} Data

Suppose you want to store 50 records in the file you would create 50 indices and 50 data structures. The 50 index structures would be written to the file first, followed by the 50 data structures.

To read the file you would read in the 50 index structures, then from the data in the read-in index structures you could tell where to "seek" to read the data records.

Look up (fopen, fread, fwrite, fclose, ftell) for functions to read/write the data.

(Sorry my semicolon key doesn't work)