2011/8/18 Andrew Yancy <andrew732@yahoo.com>
I've used the same boilerplate boost::asio code for years to send a plain text file "foo.txt" from a server back to a client and it has always worked fine. Suddenly for no apparent reason, the same code I've always used with no problems now deletes the contents of foo.txt before it can actually be sent to the client. I'm creating foo.txt with a different program now, but I don't see how that could make any difference on Win32.
Below is the relevant part of the code just before I use boost::asio::write to send the contents of foo.txt back to the client. As shown by the two printf statements, the contents of the file are somehow lost when the file name and size are sent to the client. Anyone know what's going on? Is there any other information I can provide to shed light on the problem? Thanks very much for any help~
std::ifstream source_file("foo.txt", std::ios_base::binary | std::ios_base::ate);
if (!source_file)
return __LINE__;
size_t file_size = source_file.tellg();
source_file.seekg(0);
printf("foo.txt size: %d\n", (int) file_size); //size is OK here
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << return_fn << "\n" << file_size << "\n\n";
boost::asio::write(socket, request);
file_size = source_file.tellg();
printf("foo.txt size: %d\n", (int) file_size); //size is now 0!
I see no relation between asio and your source_file.
I only see that you called "file_size = source_file.tellg()" after "source_file.seekg(0)".