K
KL
I have a problem trying to write to a binary file that I want to name
after a particular name from a set. My code for the function follows,
please excuse the horrible mistakes you may see, I am a student, and
trying my best.
void retrieveImage(set<string>finalset, string hostname, string filename){
set<string>::iterator i;
string img, filetoget;
char test[256];
for (i = finalset.begin();i != finalset.end();i++){
img = *i;
filetoget = filename + img;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}
cout<< "Socket created..." << endl;
struct sockaddr_in server;
struct hostent *host = gethostbyname(hostname.c_str());
if (host == NULL) {
cout << "gethostbyname failed" << endl;
exit(1);
}
server.sin_family = host->h_addrtype;
server.sin_addr.s_addr = ( (struct in_addr *) (host->h_addr)
)->s_addr;
server.sin_port = htons(80);
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
cout << "connect failed" << endl;
exit(1);
}
char response[1024], finalresponse[1024];
for(int z=0; z<1024;++z){
response[z] = '\0';
finalresponse[z] = '\0';
}
string request;
request = "GET " + filetoget + " HTTP/1.1\015\012Host:" + hostname +
"\015\012Connection:close\015\012\015\012";
send(sock, request.c_str(), request.length() , 0);
int length = recv(sock, response, 1023, 0);
string response1 = response, endhead = "\015\012\015\012";
int c = response1.find(endhead), d=0;
while (c < 1024){
finalresponse[d] = response1[c];
d++;
c++;
}
char imgname[256];
imgname = *i;
ofstream imgfile;
imgfile.open(imgname, ofstream::binary );
imgfile.write(finalresponse, length);
imgfile.close();
close(sock);
}
}
after a particular name from a set. My code for the function follows,
please excuse the horrible mistakes you may see, I am a student, and
trying my best.
void retrieveImage(set<string>finalset, string hostname, string filename){
set<string>::iterator i;
string img, filetoget;
char test[256];
for (i = finalset.begin();i != finalset.end();i++){
img = *i;
filetoget = filename + img;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "cannot create socket" << endl;
exit(1);
}
cout<< "Socket created..." << endl;
struct sockaddr_in server;
struct hostent *host = gethostbyname(hostname.c_str());
if (host == NULL) {
cout << "gethostbyname failed" << endl;
exit(1);
}
server.sin_family = host->h_addrtype;
server.sin_addr.s_addr = ( (struct in_addr *) (host->h_addr)
)->s_addr;
server.sin_port = htons(80);
if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
cout << "connect failed" << endl;
exit(1);
}
char response[1024], finalresponse[1024];
for(int z=0; z<1024;++z){
response[z] = '\0';
finalresponse[z] = '\0';
}
string request;
request = "GET " + filetoget + " HTTP/1.1\015\012Host:" + hostname +
"\015\012Connection:close\015\012\015\012";
send(sock, request.c_str(), request.length() , 0);
int length = recv(sock, response, 1023, 0);
string response1 = response, endhead = "\015\012\015\012";
int c = response1.find(endhead), d=0;
while (c < 1024){
finalresponse[d] = response1[c];
d++;
c++;
}
char imgname[256];
imgname = *i;
ofstream imgfile;
imgfile.open(imgname, ofstream::binary );
imgfile.write(finalresponse, length);
imgfile.close();
close(sock);
}
}