<p>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. </p>

<p>My question is this:</p>

<p>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?</p>

[{'answer_id': 74124, 'author': 'Vhaerun', 'author_id': 11234, 'author_profile': 'https://Stackoverflow.com/users/11234', 'pm_score': 0, 'selected': False, 'text': '<p>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 .</p>\n'}, {'answer_id': 74131, 'author': 'Frank Wiles', 'author_id': 12568, 'author_profile': 'https://Stackoverflow.com/users/12568', 'pm_score': 0, 'selected': False, 'text': "<p>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.  </p>\n\n<p>Hope that helps you find what you're looking for!</p>\n"}, {'answer_id': 74132, 'author': 'cazlab', 'author_id': 6178, 'author_profile': 'https://Stackoverflow.com/users/6178', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 74134, 'author': 'David Thibault', 'author_id': 5903, 'author_profile': 'https://Stackoverflow.com/users/5903', 'pm_score': 2, 'selected': False, 'text': "<p>You basically have to come up with your own file format and write binary data.\nYou can also serialize your object model and write the output to a file, but that's usually less efficient.</p>\n\n<p>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.</p>\n"}, {'answer_id': 74139, 'author': 'Marc Gear', 'author_id': 6563, 'author_profile': 'https://Stackoverflow.com/users/6563', 'pm_score': 2, 'selected': False, 'text': '<p>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).  </p>\n\n<p>Or (<em>shudder</em>) you could come up with your own propriatory binary format, and use that.</p>\n'}, {'answer_id': 74153, 'author': 'Jean', 'author_id': 7898, 'author_profile': 'https://Stackoverflow.com/users/7898', 'pm_score': 2, 'selected': False, 'text': '<p>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. \nfor a spreadsheet application you could even use a text format (OOXML, OpenDocument) to store presentation and content information. </p>\n\n<p>Or you could define binary datastructures and write that directly to the file.</p>\n\n<p>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.</p>\n'}, {'answer_id': 74160, 'author': 'Tom Ritter', 'author_id': 8435, 'author_profile': 'https://Stackoverflow.com/users/8435', 'pm_score': 2, 'selected': False, 'text': '<p>You have to know the binary file format for the file you are trying to create.  Consider <a href="http://www.joelonsoftware.com/items/2008/02/19.html" rel="nofollow noreferrer">Joel\'s post on this topic</a>: the 97-2003 File Format is a 349 page spec.  </p>\n\n<p>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.</p>\n'}, {'answer_id': 74173, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 1, 'selected': False, 'text': '<p>See <a href="http://www.wotsit.org/" rel="nofollow noreferrer">wotsit.org</a> 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.</p>\n\n<p>Writing to a database can be done by using a wrapper class in your language, mainly passing it SQL commands. </p>\n'}, {'answer_id': 74177, 'author': 'Nerdfest', 'author_id': 7855, 'author_profile': 'https://Stackoverflow.com/users/7855', 'pm_score': 0, 'selected': False, 'text': '<p>Use xml (something open, descriptive, and validatable), and stick with the text. There are standards for this sort of thing as well, including <a href="http://en.wikipedia.org/wiki/OpenDocument" rel="nofollow noreferrer">ODF</a></p>\n'}, {'answer_id': 74182, 'author': 'foxxtrot', 'author_id': 10369, 'author_profile': 'https://Stackoverflow.com/users/10369', 'pm_score': 0, 'selected': False, 'text': '<p>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 <a href="http://en.wikipedia.org/wiki/Endianess" rel="nofollow noreferrer">endianess</a>, which can become an issue when moving the files from one architecture to another (x86 to PPC for instance).</p>\n\n<p>Writing binary data to disk is really no harder than writing text, and really, your creativity is key for how you store the data.</p>\n'}, {'answer_id': 74189, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>/Allan</p>\n'}, {'answer_id': 74240, 'author': 'Eclipse', 'author_id': 8701, 'author_profile': 'https://Stackoverflow.com/users/8701', 'pm_score': 0, 'selected': False, 'text': '<p>There are a variety of approaches you can take, but in general you\'ll want some sort of serialization library.  BOOST::Serialization, or <a href="http://code.google.com/apis/protocolbuffers/" rel="nofollow noreferrer" title="Google&#39;s Protocal Buffers">Google\'s Protocal Buffers</a> 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.</p>\n\n<p>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 <a href="http://msdn.microsoft.com/en-us/library/c565h7xx.aspx" rel="nofollow noreferrer">fopen, fread, fwrite and fclose</a> for a starting point.</p>\n'}, {'answer_id': 382786, 'author': 'user21826', 'author_id': 21826, 'author_profile': 'https://Stackoverflow.com/users/21826', 'pm_score': 0, 'selected': False, 'text': '<p>A typical binary file format for custom data is an "indexed file format" consisting of</p>\n\n<pre>\n-------\n|index|\n-------\n|data |\n-------\n</pre>\n\n<p>Where the index contains records "pointing" to the data.</p>\n\n<p>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).</p>\n\n<pre>\ntypedef struct {\n  size_t offset\n  size_t size\n} Index\n\ntypedef struct {\n  int  ID\n  char First[20]\n  char Last[20]\n  char *RandomInfo\n} Data\n</pre>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>Look up (fopen, fread, fwrite, fclose, ftell) for functions to read/write the data.</p>\n\n<p>(Sorry my semicolon key doesn\'t work)</p>\n'}, {'answer_id': 22926815, 'author': 'icedwater', 'author_id': 1091386, 'author_profile': 'https://Stackoverflow.com/users/1091386', 'pm_score': 0, 'selected': False, 'text': '<p>1985 called, and said they have some help IFF you are willing to read up. The <a href="http://en.wikipedia.org/wiki/Interchange_File_Format" rel="nofollow">interchange file format</a> is still in use today and provides some basic metadata around binary files, such as RIFF or WAV audio. (Unfortunately, TIFF is a false friend.) It <a href="http://en.wikipedia.org/wiki/Interchange_File_Format" rel="nofollow">allegedly</a> even inspired PNG, so it can\'t be that bad.</p>\n'}]
['https://Stackoverflow.com/questions/74116', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/73/']