set<string>::iterator i to binary filename?

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);
}

}
 
M

Moonlit

Hi,

I didn't go through all your code but see below.

--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

KL said:
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;
-------------------------------------------------------------------------------->
Why not use string?

string imgname;
imgname = *i;
imgfile.open( imgname.c_str(), ios_base::binary );
 
T

TB

KL skrev:
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){

Better use a [const] reference to 'finalset', thus avoiding any
unnecessary copying, unless you actually wish to copy the entire object.

<snip>
 
D

Daniel T.

KL said:
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.

You could have paired your code down some more:

void fn( set< string >::iterator i ) {
char imgname[256];
imgname = *i;
ofstream imgfile;
imgfile.open(imgname, ofstream::binary );
}

The above reports:

error: incompatible types in assignment of 'const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
to 'char [256]'

IE: you cannot assign a string to a char buffer. Do this instead:

void fn( set< string >::iterator i ) {
ofstream imgfile;
imgfile.open(i->c_str(), ofstream::binary );
}
 
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.


You could have paired your code down some more:

void fn( set< string >::iterator i ) {
char imgname[256];
imgname = *i;
ofstream imgfile;
imgfile.open(imgname, ofstream::binary );
}

The above reports:

error: incompatible types in assignment of 'const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
to 'char [256]'

IE: you cannot assign a string to a char buffer. Do this instead:

void fn( set< string >::iterator i ) {
ofstream imgfile;
imgfile.open(i->c_str(), ofstream::binary );
}
Thanks to all...I got that to work, now I just can't figure why my
binary files aren't the right thing....sigh

Supposed to end up being .gif files, but they aren't. Am pulling my
hair out now.
 
M

Moonlit

Hi,

I believe http sometimes sends all the information in one block and
sometimes it sends small blocks. Somewhere at the beginning is the length of
the block to come. Though I am not sure it happens with pcitures etc. but it
certainly happens with html pages themselve.

I usually program my recv's in a loop until I know I have got everything
(or the other side disconnnecs i.e. 0 return on a blocking port)..

--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

KL said:
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.


You could have paired your code down some more:

void fn( set< string >::iterator i ) {
char imgname[256];
imgname = *i;
ofstream imgfile;
imgfile.open(imgname, ofstream::binary );
}

The above reports:

error: incompatible types in assignment of 'const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to 'char [256]'

IE: you cannot assign a string to a char buffer. Do this instead:

void fn( set< string >::iterator i ) {
ofstream imgfile;
imgfile.open(i->c_str(), ofstream::binary );
}
Thanks to all...I got that to work, now I just can't figure why my binary
files aren't the right thing....sigh

Supposed to end up being .gif files, but they aren't. Am pulling my hair
out now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top