I am having problem to write a wstring to a file. I have made functions to write and read strings and wstrings but for a reason the I don't know it doesn't work for wstrings.

Here is my header
Code:
#include <string>
#include <fstream>
#ifndef _FSTRINGSTREAM_H
#define _FSTRINGSTREAM_H
void string_to_file(std::string &s, std::ofstream &fout)
{
    int len = s.length();
    len++;            // You need to write a '\0' at the end of a char*
    fout.write(reinterpret_cast<char *> (&len), sizeof(int));
    fout.write(s.c_str(), len);
}

std::string file_to_string(std::ifstream &fin)
{
    int len;
    fin.read(reinterpret_cast<char *> (&len), sizeof(int));
    char *str_s = new char[len];
    fin.read(str_s, len);
    std::string s = str_s;
    delete str_s;
    return s;
}

void wstring_to_file(std::wstring &w, std::wofstream &wfout)
{
    int len = w.length();
    len++;            // You need to write a '\0' at the end of a wchar_t*
    wfout.write(reinterpret_cast<wchar_t *> (&len), sizeof(int));
    wfout.write(w.c_str(), len);
}

std::wstring file_to_wstring(std::wifstream &wfin)
{
    int len;
    wfin.read(reinterpret_cast<wchar_t *> (&len), sizeof(int));
    wchar_t *str_w = new wchar_t[len];
    wfin.read(str_w, len);
    std::wstring w = str_w;
    delete str_w;
    return w;
}
#endif
And here is the file I am testing on:
Code:
#include <iostream>
#include <string>
#include <fstream>
#ifndef _FSTRINGSTREAM_H
#include "fstringstream.h"
#endif
using namespace std;

int main()
{
    wofstream fout("testfile.dat");
    if ( !fout.is_open() )
    {
        cerr << "ERROR: can't open file";
        exit(1);
    }
    
    wstring s = L"test";
    wstring_to_file(s, fout);
    fout.close();
    
    wifstream fin("testfile.dat");
    if ( !fin.is_open() )
    {
        cerr << "ERROR: can't open file";
        exit(1);
    }
    
    s = file_to_wstring(fin);
    fin.close();
    wcout << s;
    
    return 0;
}
The program gets compiled without problems with Borland C++ compiler 5.5.1 but if I run the program, I get the text "Abnormal program termination". The first time I run the program (and the first time after restarting the computer) Windows gives me the message that there is not enough virtual memory available.