When using read() to read a binary file's data, it only gets the first byte, like: ÿû’D
Does anyone know how I might be able to fix this?
When using read() to read a binary file's data, it only gets the first byte, like: ÿû’D
Does anyone know how I might be able to fix this?
That's more than a byte. May we see some code?
Code:filename = "encrypter.exe"; ifstream file(filename, ios::in|ios::binary|ios::ate); if(file.is_open()) { char* memblock = new char[filesize]; int filesize = thefile.tellg(); file.seekg(0, ios::beg); file.read(memblock, filesize); file.close(); }
Woopsies.Code:char* memblock = new char[filesize]; int filesize = thefile.tellg();
that isn't the problem. that mistake was made while posting the code(filesize is declared and value is set before memblock).
May I see the code that is sitting in your IDE or text editor at this very moment which is causing troubles.
This sends 256 bytes which is more then your filesize_char variable. You can use strlen() to get the correct size.Code:send(mysocket, filesize_char, 256, 0);
Also if you want to send the filesize as text then the receiver needs a way of knowing when the filesize ends and the file data begins such as a newline charactor.
Code:filename = new char[256]; filename = "encrypt.exe";And then delete[] the allocated memory somewhere. Or don't allocate it like that.Code:filename = new char[256]; strcpy(filename, "encrypt.exe");
It's only 50 bytes. And it may be unintended, that's not the filename.Code:send(mysocket, filesize_char, 256, 0); // send the file name
Use only one filehandle, thefile or file.
thanks, but that stuff isn't the problem. I can succesfully open a text file, and send it. But when trying to open a bin file(like "encrypt.exe"), it only reads a small ammount.
How do you know that it isn't reading the whole file? For example:Code:file.read(memblock, filesize); file.close(); std::cout<<memblock; //memblock isn't null-terminated! //you'd have to do this instead: for (int i=0;i<filesize;i++) std::cout<<memblock[i];
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
When the buffer reaches the other end, I write the buffer to file. And only a small ammount of the original file is sent and written.
Looking at your code, I don't see why you open the file twice with two separate ifstreams (it's especially confusing since you use similar names for both of them). I don't really think that's the problem though.
How do you really know that all the data is sent? You don't even check the return value of send(). Also, have you checked the other data that is sent to be sure it's correct (e.g., the file length)? And how exactly are you writing to the file on the other end (e.g., using the write() member function)?
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
The problem is not with the other end, the problem is with read(). The buffer without being sent over sockets, still is only like 3 bytes.