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

Discussion in 'C++' started by KL, Mar 12, 2006.

  1. KL

    KL Guest

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

    }
    KL, Mar 12, 2006
    #1
    1. Advertising

  2. KL

    Moonlit Guest

    Hi,

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

    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "KL" <> wrote in message
    news:...
    >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 );

    > imgfile.open(imgname, ofstream::binary );
    > imgfile.write(finalresponse, length);
    > imgfile.close();
    > close(sock);
    > }
    >
    > }
    Moonlit, Mar 12, 2006
    #2
    1. Advertising

  3. KL

    TB Guest

    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>

    --
    TB @ SWEDEN
    TB, Mar 12, 2006
    #3
  4. KL

    Daniel T. Guest

    In article <>,
    KL <> wrote:

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

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Mar 13, 2006
    #4
  5. KL

    KL Guest

    on 3/12/2006 8:20 PM Daniel T. said the following:
    > In article <>,
    > KL <> wrote:
    >
    >
    >>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.

    --

    KL
    KL, Mar 13, 2006
    #5
  6. KL

    Moonlit Guest

    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" <> wrote in message
    news:D...
    > on 3/12/2006 8:20 PM Daniel T. said the following:
    >> In article <>,
    >> KL <> wrote:
    >>
    >>
    >>>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.
    >
    > --
    >
    > KL
    Moonlit, Mar 14, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,005
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,437
    Roland de Ruiter
    Jun 15, 2006
  3. Ed
    Replies:
    10
    Views:
    45,744
    alok000707
    Jul 13, 2010
  4. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    734
    Jonathan N. Little
    May 30, 2008
  5. Bergamot
    Replies:
    0
    Views:
    435
    Bergamot
    May 30, 2008
Loading...

Share This Page