Hi i have been making two programs: one that sends a file's filebuf made into a char*, and another that receives that and writes it to a file. They're like exe sending programs. I can send the char* and write it onto the console using cout.write(), but when I try to copy it onto a file, it will only be like 3 bytes in size while the original file is 500kb. How would I write it correctly? I don't want to show all of my code because that would take up space, and most of it is irrelevant. I will just show the sending, receiving, and writing.
Sending:
ifstream in(FileName,ios::binary);
if (in)
{
filebuf *pbuf;
pbuf = in.rdbuf();
long size;
size = pbuf->pubseekoff(0,ios::end,ios::in);
char * buffer;
buffer = new char[size];
pbuf->pubseekpos(0,ios::in);
pbuf->sgetn(buffer,size);
if (send(sClient, buffer, strlen(buffer), 0) != SOCKET_ERROR)
{
cout << "\nSent file successfully!";
}
else
{
cout << "\nError sending file!\n";
}
}
Receiving and Writing:
char cClientMessage[500000];
recv(sClient, cClientMessage, 499999, 0);
ofstream out(TempFileName.c_str(), ios::binary);
string ConfirmString;
if (out)
{
out.write(cClientMessage, strlen(cClientMessage));
ConfirmString = "Successfully copied file! New file name is ";
ConfirmString += TempFileName;
out.close();
}
else
{
ConfirmString = "Error copying file!";
}
send(sClient, ConfirmString.c_str(), ConfirmString.length(), 0);
}