After I have written into a file when reading it I get unexpected output.
The code I wrote is :
#include<fstream.h>
#include<conio.h>
#include<string.h>
struct test
{
char que[100];
char ans[20];
};
int main()
{
test s, d;
clrscr();
ofstream out("test.dat", ios::binary | ios::app);
ifstream in("test.dat", ios::binary | ios::in);
strcpy(s.que, "2.How many ways the letters of the word abas be arranged to form words with or without meaning");
strcpy(s.ans, "180");
out.write((char*) &s, sizeof(test));
while(!in.eof())
{
in.getline((char*) &d, sizeof(test));
cout << d.que << '\n' << d.ans;
}
getch();
return 0;
}
The output that I get is :
2.How many ways the letters of the word abas be arranged to form words with or w
ithout meaning
180
180
This is the output I get along with some arbitrary characters in between.
What have I done wrong? Why is the string that I stored in s.ans
written into s.que
as well?