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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KL

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

    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(s et<string>final set, string hostname, string filename){
    set<string>::it erator 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(h ostname.c_str() );
    if (host == NULL) {
    cout << "gethostbyn ame failed" << endl;
    exit(1);
    }
    server.sin_fami ly = 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\012Connec tion: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\0 12";
    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(im gname, ofstream::binar y );
    imgfile.write(f inalresponse, length);
    imgfile.close() ;
    close(sock);
    }

    }
  • Moonlit

    #2
    Re: set&lt;string&g t;::iterator i to binary filename?

    Hi,

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

    --


    Regards, Ron AF Greve



    "KL" <klbjornme@aohe ll.com> wrote in message
    news:08GdnejAFJ j_8InZRVn-qg@telcove.net. ..[color=blue]
    >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(s et<string>final set, string hostname, string filename){
    > set<string>::it erator 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(h ostname.c_str() );
    > if (host == NULL) {
    > cout << "gethostbyn ame failed" << endl;
    > exit(1);
    > }
    > server.sin_fami ly = 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\012Connec tion: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\0 12";
    > int c = response1.find( endhead), d=0;
    >
    > while (c < 1024){
    > finalresponse[d] = response1[c];
    > d++;
    > c++;
    > }
    >
    > char imgname[256];
    > imgname = *i;
    > ofstream imgfile;[/color]
    -------------------------------------------------------------------------------->
    Why not use string?

    string imgname;
    imgname = *i;
    imgfile.open( imgname.c_str() , ios_base::binar y );
    [color=blue]
    > imgfile.open(im gname, ofstream::binar y );
    > imgfile.write(f inalresponse, length);
    > imgfile.close() ;
    > close(sock);
    > }
    >
    > }[/color]



    Comment

    • TB

      #3
      Re: set&lt;string&g t;::iterator i to binary filename?

      KL skrev:[color=blue]
      > 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(s et<string>final set, string hostname, string filename){[/color]

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

      <snip>

      --
      TB @ SWEDEN

      Comment

      • Daniel T.

        #4
        Re: set&lt;string&g t;::iterator i to binary filename?

        In article <08GdnejAFJj_8I nZRVn-qg@telcove.net> ,
        KL <klbjornme@aohe ll.com> wrote:
        [color=blue]
        > 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.[/color]

        You could have paired your code down some more:

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

        The above reports:

        error: incompatible types in assignment of 'const
        std::basic_stri ng<char, std::char_trait s<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::binar y );
        }

        --
        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.

        Comment

        • KL

          #5
          Re: set&lt;string&g t;::iterator i to binary filename?

          on 3/12/2006 8:20 PM Daniel T. said the following:[color=blue]
          > In article <08GdnejAFJj_8I nZRVn-qg@telcove.net> ,
          > KL <klbjornme@aohe ll.com> wrote:
          >
          >[color=green]
          >>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.[/color]
          >
          >
          > You could have paired your code down some more:
          >
          > void fn( set< string >::iterator i ) {
          > char imgname[256];
          > imgname = *i;
          > ofstream imgfile;
          > imgfile.open(im gname, ofstream::binar y );
          > }
          >
          > The above reports:
          >
          > error: incompatible types in assignment of 'const
          > std::basic_stri ng<char, std::char_trait s<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::binar y );
          > }
          >[/color]
          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

          Comment

          • Moonlit

            #6
            Re: set&lt;string&g t;::iterator i to binary filename?

            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



            "KL" <klbjornme@aohe ll.com> wrote in message
            news:DZWdnfYF35 OXQInZRVn-tg@telcove.net. ..[color=blue]
            > on 3/12/2006 8:20 PM Daniel T. said the following:[color=green]
            >> In article <08GdnejAFJj_8I nZRVn-qg@telcove.net> ,
            >> KL <klbjornme@aohe ll.com> wrote:
            >>
            >>[color=darkred]
            >>>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.[/color]
            >>
            >>
            >> You could have paired your code down some more:
            >>
            >> void fn( set< string >::iterator i ) {
            >> char imgname[256];
            >> imgname = *i;
            >> ofstream imgfile;
            >> imgfile.open(im gname, ofstream::binar y );
            >> }
            >>
            >> The above reports:
            >>
            >> error: incompatible types in assignment of 'const std::basic_stri ng<char,
            >> std::char_trait s<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::binar y );
            >> }
            >>[/color]
            > 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[/color]


            Comment

            Working...