CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2007
    Posts
    9

    istream::get(char*, int) problem

    First please don't waste your time and my time by telling me to store my images on disk instead of a database. There are advantages to both and in this case it is much easier to manage these images and their classification by using a database to store all the data (I'm not storing many large images anyway, just 4 or 5 small jpegs that get updated now and then).

    Problem:
    Now I need to know if I'm using istream::get(char*, int) in a naive way or if there is probably some inherit bug in sql::ResultSet::getBlob(string) that I should report. What I mean in a "naive way" is that is there no guarantee that I wont hit the 'delim' char in the middle of my stream if I'm reading in data that is representing a jpeg? Do I need to use the istream::get(char*,int,char) where the last char is the 'delim'? I played around with this code for awhile before finally finding that sql::ResultSet::getString(string) and converting it to a c string worked for my purposes.

    Code:
      //Notes: result is a result set from a MySQL api (MySQL C++/Connector)
      //           "Master" is a jpeg stored as a longblob in a MySQL database
      //           "MasterSize" is the jpeg's size in bytes stored as an int in a MySQL database
    
      ///////////////////////////////////////////////////////////////////////////
      //Code that does not work:  buffer does not have the
      //data in it
      int size = result->getInt("MasterSize")/sizeof(char);
      char* buffer = new char[size];
      
      //Note: sql::ResultSet::getBlob(string) returns an istream
      (result->getBlob("Master"))->get(buffer,size);
      ///////////////////////////////////////////////////////////////////////////
      
      ///////////////////////////////////////////////////////////////////////////
      //Code that does work
      int size = result->getInt("MasterSize")/sizeof(char);
      //Note: sql::ResultSet::getString(string) returns a std::string
      char* buffer = (result->getString("Master")).c_str();
      
      //I can then traverse buffer using size just fine and save
      //this buffer to a file to get back my jpeg.  
      ///////////////////////////////////////////////////////////////////////////
    Last edited by Graphic; September 8th, 2009 at 11:01 AM. Reason: Readibility

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: istream::get(char*, int) problem

    If you don't want any delimiter to be a meaningful place to stop reading, then you should be using istream::read() rather than istream::get().

  3. #3
    Join Date
    Oct 2007
    Posts
    9

    Re: istream::get(char*, int) problem

    Thank you! That was it, I'm guessing it was hitting a delim character when I was using get, but using read worked great. Sorry I'm still really new at C++.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured