-
copying jpeg files
I need to make a copy of a jpeg file is there any way that i could be able to do that. I eventually need to be able to send it through a socket. It would be great if there was some way that i could send any type of file through a socket. If anyone can point me in the right direction, it would be great.
Thank you
-
I'm assuming you mean that you want to do that programatically through C++, because I know you know you can copy + paste...
you could try opening it as a binary file.
Code:
//iostream for standard console I/O
#include <iostream>
//fstream for standard file I/O
#include <fstream>
int main()
{
//used to hold a small part of the file... use sizeof(char) to find out how many
//bytes are used on your system. you can use pretty much any data type here.
char ch;
//open the input file for binary reading
std::fstream infile("mitch.jpg",std::ios::binary|std::ios::in);
//open the output file for binary writing
std::fstream ofile("test.jpg",std::ios::binary|std::ios::out|std::ios::trunc);
//loop through the file, reading a small part with each iteration
while(infile.read(reinterpret_cast<char*>(&ch),sizeof(ch)))
{
//write that small part to the output file
ofile.write(reinterpret_cast<char*>(&ch),sizeof(ch));
}
//explicitly close the input file
infile.close();
//explicitly close the output file
ofile.close();
//just a little message
std::cout<<"File Written\n";
//end of the program
return 0;
}
edit: not that you need it, but here's the picture I used: http://www.osei.noaa.gov/Events/Trop...tch299I_G8.jpg
-
When i make a copy of the file on one computer it works perfect but when i try to send the characters accross a socket, the data seems to get corrupted. I want to read all the information from a file into a character array and then I want to reassemble it back together on the other end(after sending it through the socket). Text files work perfect but other file types I have a problem, how can i read all of the file data into a character array
-
the same way... except using a char array...
-
i am currently doing that but the data still gets corrupted or something, because the contents of the file i copy do not match the original. Should i be using winsock version two rather than version one. Or is there something better and faster than winsock.
-
Rawr. The TramsmitFile API: http://msdn.microsoft.com/library/de...smitfile_2.asp
Also, just for concept, here's this extremely biased Windows C code that has printf's and WinAPI calls and other cruel nasty things that I used to do the same thing as you, transfer files, and I mean, I don't even use proper winsock2 function calls. How improper of me! Just std::cout and all the other nifty io/f stream things you want to do in substitution.
Code:
while (true)
{
if(clientSocket=accept(serverSocket, (struct sockaddr*)&clientSocketAddr, &AddrLength))
{
printf("omgclientconnected\n");
if(hFile=CreateFile("C:\\whatever.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
{
printf("\tomgwegottohere\n");
while(true) {
BOOL bResult = ReadFile(hFile, szBuf, 1024, &dwNumRead, NULL);
if( bResult == 0 || dwNumRead == 0 )
{
break;
}
send(clientSocket, szBuf, strlen(szBuf),0);
memset(szBuf,0,1024);
printf("\troflbuffersent\n");
}
}
else printf("\tomfgfailure\n");
printf("\tomgwhatnow\n");
closesocket(clientSocket);
CloseHandle(hFile);
}
}
I don't know about better, or faster, I'm sure raw sockets would be enjoyable experience for you, but I would personally just stick to the provided winsock lib's. I don't understand what you mean by this corruption either. I mean, I guess UDP has been renowned for being "less reliable" in that packets are not guaranteed to arrive in the right order or at all, but we should just be using TCP streams here right? Maybe you want to elaborate on your problem?
-
-
Can any body give me some sample code that utilizes winsock2 to send messages ( a client - server format). i went to msdn and tried some code but when i ran it, it told me that the bind function failed.
-
has any body developed a code that uses the tranmitfile() function, below is
-
Has anyone developed code that uses the transmitfile() function that is in the winsock2.h header file. Do i set it it up the same as any other socket.
Code:
BOOL TransmitFile(
SOCKET hSocket,
HANDLE hFile,
DWORD nNumberOfBytesToWrite,
DWORD nNumberOfBytesPerSend,
LPOVERLAPPED lpOverlapped,
LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
DWORD dwFlags
);