I am sending array of 5 strings into a function that will write them into a binary file. My problem is how can I do that? How can I check the size of array if I am sending it by a pointer? I included #include <string> and using namespace std;.
void my_func(string *array, int n)
{
ofstream my_file("file.bin", ios_base::binary);
if (!my_file)
{
cout << "error" << endl;
return;
}
my_file.write((char*)array, n*sizeof(string));
my_file.close();
}
void fun1()
{
string array[5];
for (int i = 0; i < 5; i++)
{
cout << "enter word " << i + 1 << ": ";
getline(cin, array[i]);
}
my_func(array,5);
return;
}
So this works, but I use too many bytes for nothing, I want to take correct amount of bytes. Or if there is an easy way to do all this, I would be thankful. And please give me a school example, I am a student.
Output:

stringdefined?std::stringto the file instead of its contents.stringisstd::string, Lingxi. The OP hasn't given enough information to conclude that.