Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems With Files 1

Status
Not open for further replies.

ForeverCode

Programmer
Dec 6, 2002
41
US
File.Open(RFile, CFile::shareDenyNone | CFile::typeBinary, &pError)
UINT nRead;

nRead = File.Read(FileData, 8192);

while (nRead == 8192){
AllData.Format("%s", FileData);

send(work, AllData, 8192, 0);
nRead = File.Read(FileData, nRead);
}

if( nRead > 0 ){
AllData.Format("%s", FileData);

send(work, AllData, nRead, 0);
}


With the above code I can open text files fine, but if I try to open anything else(gif, jpg, zip, etc.) and send the data through the socks the data is all messed up...

I think it is because the CString terminating at the NULLs in the file but im not sure...Anyone know how to fix this problem?
 
Use "%d" or "%x" format for each byte (word) of your data instead of "%s". The server side must pack the data vice versa.
 
It didn't work:( Thanx for the help anyways, I think you're on the right track there.
 
You should allocate a buffer of an appropriate type for the appropriate length. In your case, you know the length is 8192 bytes - if you didn't know the length then you can use [tt]CFile::GetLength()[/tt] to get it.

[tt]File.Open(RFile, CFile::shareDenyNone | CFile::typeBinary, &pError)
UINT nRead;

// allocate a buffer to hold the data
char* buffer = (char*)new char[8192];

nRead = File.Read(buffer, 8192);[/tt]

Now you can safely pass the buffer to a socket and you shouldn't have any problems.

When you're finished with the buffer, don't forget to delete it:

[tt]delete []buffer;[/tt]

Hope this helps!

[rockband]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Great!

[2thumbsup]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top