Writing/reading data structure to a file using C++ I wrote some piece of code which reads and write multiple data structures on a file
using C++. I would be grateful to get your feedback on what you think about the code (it works at least when I tested). Thanks. 

#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
using namespace std;

// data structure to be written to a file 
struct WebSites
{
    char SiteName[100];
    int Rank;
}s1,s2,s3,s4;


int _tmain(int argc, _TCHAR* argv[])
{
    strcpy(s1.SiteName, "www.ppp.com");
    s1.Rank = 0;

    strcpy(s2.SiteName, "www.rrr.com");
    s2.Rank = 111;

    strcpy(s3.SiteName, "www.code.com");
    s3.Rank = 123;

    strcpy(s4.SiteName, "www.yahoo.com");
    s4.Rank = 14;

    // write
    fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
    binary_file.write(reinterpret_cast&lt;char *&gt;(&amp;s1),sizeof(WebSites));
    binary_file.write(reinterpret_cast&lt;char *&gt;(&amp;s2),sizeof(WebSites));
    binary_file.write(reinterpret_cast&lt;char *&gt;(&amp;s3),sizeof(WebSites));
    binary_file.write(reinterpret_cast&lt;char *&gt;(&amp;s4),sizeof(WebSites));
    binary_file.close();

    // Read data
    fstream binary_file2("c:\\test.dat",ios::binary|ios::in| ios::ate );
    int size = binary_file2.tellg();
    for(int i = 0; i&lt;size/sizeof(WebSites); i++)
    { 
        WebSites p_Data;
        binary_file2.seekg(i*sizeof(WebSites));
        binary_file2.read(reinterpret_cast&lt;char *&gt;(&amp;p_Data),sizeof(WebSites));

        cout&lt;&lt;p_Data.SiteName&lt;&lt;endl;
        cout&lt;&lt;"Rank: "&lt;&lt; p_Data.Rank&lt;&lt;endl;
    }
    binary_file2.close();

    return 0;
}
The trouble with writing binary blobs is that they lead to brittle storage.

The stored objects have a tendency to break over time as the assumptions you make about the hardware no longer hold true (in this case that the sizeof(int) is constant and the endianess of int will not change).

It has become more standard therefore to use a method know as serialization. In this you convert the object to a format that is hardware agnostic (and usually human readable).

Note: Binary blobs have advantages. But you must weigh those against the brittleness. Therefore your first choice should be serialization (unless you have specific requirements that prevents this). Then look at binary Blobs only after you have shown that serialization has too much overhead (unlikely for most situations, but it is a possibility).

In C++ you would do this using the operator&lt;&lt; and operator&gt;&gt;.

I would re-write your code as:

struct WebSites
{
    std::string siteName;
    int rank;

    WebSite()
        : siteName("")
        , rank(0)
    {}

    WebSite(std::string const&amp; siteName, int rank)
        : siteName(siteName)
        , rank(rank)
    {}
    void swap(WebSites&amp; other) throws()
    {
        std::swap(rank,     other.rank);
        std::swap(siteName, other.siteName);
    }
};
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, WebSites const&amp; data)
{
    stream &lt;&lt; data.rank &lt;&lt; " " 
           &lt;&lt; data.siteName.size() &lt;&lt; ":" 
           &lt;&lt; data.siteName;
    return stream;
}
std::istream&amp; operator&gt;&gt;(std::istream&amp; stream, WebSites&amp; data)
{
    WebSite     tmp;
    std::size_t size;
    if (stream &gt;&gt; tmp.rank &gt;&gt; size)
    {
        tmp.siteName.resize(size);
        if (stream.read(tmp.siteName[0], size)
        {
            data.swap(tmp);
        }
    }
    return stream;
}


Now you can write your code like this:

int _tmain(int argc, _TCHAR* argv[])
{
    WebSite s1("www.ppp.com", 0);    
    WebSite s2("www.rrr.com", 111);
    WebSite s3("www.code.com", 123);
    WebSite s4("www.yahoo.com", 14);

    // write
    fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
    binary_file &lt;&lt; s1 &lt;&lt; s2 &lt;&lt; s3 &lt;&lt; s4;
    binary_file.close();

    // Read data
    fstream binary_file2("c:\\test.dat",ios::binary|ios::in| ios::ate );
    WebSites p_Data;
    while(binary_file2 &gt;&gt; p_Data)
    { 
        cout &lt;&lt; p_Data.SiteName &lt;&lt; endl;
        cout &lt;&lt; "Rank: "&lt;&lt; p_Data.Rank &lt;&lt; endl;
    }
    binary_file2.close();

    // Read data into a vector
    fstream binary_file3("c:\\test.dat",ios::binary|ios::in| ios::ate );
    std::vector&lt;WebSites&gt; v;

    std::copy(std::istream_iterator&lt;WebSites&gt;(binary_file3),
              std::istream_iterator&lt;WebSites&gt;(),
              std::back_inserter(v)
             );
    binary_file3.close();
}