character pointers in C/C++ must be null terminated. That means you must allot another character with value of '\0' at the end.
Also note, strcpy
function copies all the characters from one string to another, until \0
is encountered, unless its a const string(an example is "hello world") which is stored as "hello world\0" during compilation.
Try this code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <fstream.h>
#include <stdio.h>
class Test
{
public:
char mobile[11], address[30];
};
void main()
{
clrscr();
Test t;
// uncoment below to write to file
strcpy(t.mobile, "1234567890");
strcpy(t.address, "Mumbai");
t.address[10] = '\0';
fstream f("_test.bin", ios::binary | ios::out | ios::app);
f.write((char*)&t, sizeof(t))
// uncomment below to read from file
fstream f("_test.bin", ios::binary | ios::in);
f.read((char*)&t, sizeof(t));
cout << t.mobile << "\t" << t.address;
f.close();
getch();
}