List Info

Thread: lostream to a file




lostream to a file
user name
2007-09-04 17:03:23
I am trying to get comfortable using the lostream class for
large 
objects.  I have a large binary file that I am storing in a
BLOB that I 
can get back out using largeobjectaccess and then use the
to_file(file) 
method to write it back out and that works fine.  However, I
want to 
give users streaming functionality, but I need to know how
to do it 
first 
.

What I would like to do first is stream this back out to a
file, similar 
to what I've already done with
largeobjectaccess::to_file(file), but 
instead I want to use lostream.

Given this code, what do I need to do next?

...
   pqxx::work w(*atr->getConn(),me);
   pqxx::ilostream s(w, id5);

   ofstream file("test.seqb", ios::out |
ios::binary | ios::trunc);
   cout << "Streaming blob into a file "
<< endl;

   //This isn't right
   if(s.good())
   {
      file << s;
   }
   cout << "Done Streaming blob into a file
" << endl;
   file.close();
...
_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general

Re: lostream to a file
user name
2007-09-04 17:13:41
Try a look on this:
http://www.cplusplus.com/doc/tutorial/files.html



On 9/4/07, Shannon Allen < shannon.allen3us.army.mil">shannon.allen3us.army.mil> wrote:
I am trying to get comfortable using the lostream class for large
objects. &nbsp;I have a large binary file that I am storing in a BLOB that I
can get back out using largeobjectaccess and then use the to_file(file)
method to write it back out and that works fine. ; However, I want to
give users streaming functionality, but I need to know how to do it
first .

What I would like to do first is stream this back out to a file, similar
to what I've already done with largeobjectaccess::to_file(file), but
instead I want to use lostream.

Given this code, what do I need to do next?

...
 &nbsp; pqxx::work w(*atr->;getConn(),me);
   pqxx::ilostream s(w, id5);

&nbsp;  ofstream file(";test.seqb&quot;, ios::out | ios::binary | ios::trunc);
   cout << "Streaming blob into a file " << endl;

&nbsp;  //This isn't right
&nbsp;  if(s.good())
 &nbsp; {
 &nbsp; &nbsp;  file << s;
 &nbsp; }
 &nbsp; cout << "Done Streaming blob into a file " << endl;
&nbsp;  file.close();
...
_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org">Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general



--
Hilton William Ganzo Perantunes
Sistemas de Informação - Universidade Federal de Santa Catarina
--
Dinheiro não traz felicidade, mas dá uma sensação tão parecida... &nbsp;-_-
Re: lostream to a file
user name
2007-09-05 14:12:05
Can you tell what I am missing?  s.tellg() returns a size of
0.  I know 
there is a largeobject there with that id, because I can do
this right 
before the below code and it works perfecltly:
  pqxx::largeobjectaccess
loGet(w,137396206,PGSTD::ios::in);
  loGet.to_file("test.seqb");


  ...
   pqxx::work w(*atr->getConn(),me);
  pqxx::ilostream s(w, 137396206);

  ofstream file("test.seqb",
ios::out|ios::binary|ios::trunc);
  pqxx::ilostream::pos_type size;
  char * memblock(0);
 
  cout << "Streaming blob into a file "
<< endl;
  if(s.good())
  {
     size = s.tellg();
     cout << "SIZE OF LOSTREAM " <<
size << endl;   ==> prints out 0
     memblock = new char [size];
     s.seekg (0, ios::beg);
     s.read (memblock, size);
     file.write(memblock,size);
     delete[] memblock;
     file.close();
  }


Thanks.

Hilton Perantunes wrote:
> Try a look on this:
> http
://www.cplusplus.com/doc/tutorial/files.html
>
>
>
> On 9/4/07, *Shannon Allen * <shannon.allen3us.army.mil 
> <mailto:shannon.allen3us.army.mil>> wrote:
>
>     I am trying to get comfortable using the lostream
class for large
>     objects.  I have a large binary file that I am
storing in a BLOB
>     that I
>     can get back out using largeobjectaccess and then
use the
>     to_file(file)
>     method to write it back out and that works fine. 
However, I want to
>     give users streaming functionality, but I need to
know how to do it
>     first  .
>
>     What I would like to do first is stream this back
out to a file,
>     similar
>     to what I've already done with
largeobjectaccess::to_file(file), but
>     instead I want to use lostream.
>
>     Given this code, what do I need to do next?
>
>     ...
>        pqxx::work w(*atr->getConn(),me);
>        pqxx::ilostream s(w, id5);
>
>        ofstream file("test.seqb", ios::out |
ios::binary | ios::trunc);
>        cout << "Streaming blob into a file
" << endl;
>
>        //This isn't right
>        if(s.good())
>        {
>           file << s;
>        }
>        cout << "Done Streaming blob into a
file " << endl;
>        file.close();
>     ...
>     _______________________________________________
>     Libpqxx-general mailing list
>     Libpqxx-generalgborg.postgresql.org
>     <mailto:Libpqxx-generalgborg.postgresql.org>
>     http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general
>
>
>
>
> -- 
> Hilton William Ganzo Perantunes
> Sistemas de Informação - Universidade Federal de Santa
Catarina
> -- 
> Dinheiro não traz felicidade, mas dá uma sensação tão
parecida...  -_-
>
------------------------------------------------------------
------------
>
> _______________________________________________
> Libpqxx-general mailing list
> Libpqxx-generalgborg.postgresql.org
> http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general
>   

_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general

Re: lostream to a file
user name
2007-09-05 14:33:59
I'm not yet experienced with the library. However, some tests from test050.cxx to test065.cxx (provided with the sources) contain code samples handling large objects.

Good luck ;)



On 9/5/07, Shannon Allen < shannon.allen3us.army.mil">shannon.allen3us.army.mil> wrote:
Can you tell what I am missing?&nbsp; s.tellg() returns a size of 0. &nbsp;I know
there is a largeobject there with that id, because I can do this right
before the below code and it works perfecltly:
 &nbsp;pqxx::largeobjectaccess loGet(w,137396206,PGSTD::ios::in);
  loGet.to_file(&quot;test.seqb");


 &nbsp;...
&nbsp;  pqxx::work w(*atr->;getConn(),me);
  pqxx::ilostream s(w, 137396206);

 &nbsp;ofstream file(";test.seqb&quot;, ios::out|ios::binary|ios::trunc);
 &nbsp;pqxx::ilostream::pos_type size;
&nbsp; char * memblock(0);

 &nbsp;cout << "Streaming blob into a file " << endl;
&nbsp; if(s.good())
&nbsp; {
 &nbsp; &nbsp; size = s.tellg();
 &nbsp; &nbsp; cout << "SIZE OF LOSTREAM " << size << endl; ;  ==> prints out 0
   ;  memblock = new char [size];
&nbsp; &nbsp;  s.seekg (0, ios::beg);
 &nbsp; &nbsp; s.read (memblock, size);
&nbsp; &nbsp;  file.write(memblock,size);
 ; &nbsp;  delete[] memblock;
  ; &nbsp; file.close();
 &nbsp;}


Thanks.

Hilton Perantunes wrote:
&gt; Try a look on this:
> http://www.cplusplus.com/doc/tutorial/files.html
>
>
>
> On 9/4/07, *Shannon Allen * < shannon.allen3us.army.mil"> shannon.allen3us.army.mil
> <mailto: shannon.allen3us.army.mil">shannon.allen3us.army.mil>> wrote:
>;
; &nbsp;  I am trying to get comfortable using the lostream class for large
&gt; &nbsp; &nbsp; objects.&nbsp; I have a large binary file that I am storing in a BLOB
>&nbsp;   ; that I
>&nbsp; &nbsp;  can get back out using largeobjectaccess and then use the
>&nbsp; &nbsp;  to_file(file)
>&nbsp; &nbsp;  method to write it back out and that works fine. ; However, I want to
>&nbsp;   ; give users streaming functionality, but I need to know how to do it
>&nbsp; &nbsp;  first .
>
>; &nbsp; &nbsp; What I would like to do first is stream this back out to a file,
>  ; &nbsp; similar
&gt; &nbsp; &nbsp; to what I've already done with largeobjectaccess::to_file(file), but
>  ; &nbsp; instead I want to use lostream.
>
>&nbsp; &nbsp;  Given this code, what do I need to do next?
>
  ;  ...
>&nbsp; &nbsp;   ; &nbsp;pqxx::work w(*atr->;getConn(),me);
>&nbsp; &nbsp;   ; &nbsp;pqxx::ilostream s(w, id5);
&gt;
>&nbsp; &nbsp; &nbsp; &nbsp; ofstream file(";test.seqb&quot;, ios::out | ios::binary | ios::trunc);
>&nbsp; &nbsp; &nbsp; &nbsp; cout << "Streaming blob into a file " << endl;
>
  ; &nbsp; &nbsp; //This isn't right
&gt; &nbsp; &nbsp; &nbsp;  if(s.good())
>; &nbsp; &nbsp; &nbsp; &nbsp;{
>  ; &nbsp; &nbsp; &nbsp; &nbsp; file << s;
>&nbsp; &nbsp; &nbsp;   ;}
>&nbsp; &nbsp; &nbsp; &nbsp; cout << "Done Streaming blob into a file " << endl;
>  ; &nbsp; &nbsp; &nbsp;file.close();
>  ; &nbsp; ...
>  ; &nbsp; _______________________________________________
>&nbsp; &nbsp;  Libpqxx-general mailing list
>&nbsp;   ; Libpqxx-generalgborg.postgresql.org">Libpqxx-generalgborg.postgresql.org
  ;  <mailto: Libpqxx-generalgborg.postgresql.org">Libpqxx-generalgborg.postgresql.org>
>&nbsp; &nbsp;  http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
&gt;
>
&gt;
>
>; --
> Hilton William Ganzo Perantunes
> Sistemas de Informação - Universidade Federal de Santa Catarina
&gt; --
> Dinheiro não traz felicidade, mas dá uma sensação tão parecida... &nbsp;-_-
>; ------------------------------------------------------------------------
>;
> _______________________________________________
> Libpqxx-general mailing list
> Libpqxx-generalgborg.postgresql.org"> Libpqxx-generalgborg.postgresql.org
> http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
>;

_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org">Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general



--
Hilton William Ganzo Perantunes
Sistemas de Informação - Universidade Federal de Santa Catarina
--
Dinheiro não traz felicidade, mas dá uma sensação tão parecida... &nbsp;-_-
Re: lostream to a file
user name
2007-09-07 03:39:20
On Thu, September 6, 2007 02:12, Shannon Allen wrote:
> Can you tell what I am missing?  s.tellg() returns a
size of 0.  I know
> there is a largeobject there with that id, because I
can do this right
> before the below code and it works perfecltly:
>   pqxx::largeobjectaccess
loGet(w,137396206,PGSTD::ios::in);
>   loGet.to_file("test.seqb");

This is a C++ standard library function, so not really
anything to do with
libpqxx, but tellg() doesn't return size.  It returns your
current
position in a stream.  Which AFAICS is zero here just as it
should be.


Jeroen


_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general

Re: lostream to a file
user name
2007-09-07 05:05:22
On Thu, September 6, 2007 05:44, Shannon Allen wrote:
> Update:
>
> Getting closer.  I can now get the size of the lostream
and it matches
> the size of the file before I put it into the BLOB. 
The problem I'm
> having now is when I go to write the lostream back out
to a file, in
> Linux whenever I do a cmp originalfile newfile, it says
that they are
> different ("differ: byte 2590721, line 35"). 
But the sizes match
> exactly.  Anyone have any idea??

Hmm...  Unwanted encoding conversion maybe?  This stuff has
long been
flushed out of my mental cache, but maybe the LO stream
classes need to
add an ios::binary option somewhere.  :-/


Jeroen


_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general

Re: lostream to a file
user name
2007-09-07 11:57:44
I finally got it.  There was nothing wrong with what I was
doing at all, 
but the file I was using was a corrupted file.  Out of all
of the files 
I could have picked to test with, I picked the bad one.  Go
figure.  
Anyway, it now works.  If anyone is wandering here is some
sample code:


   std::string me = "blobStream";
   pqxx::work w(*atr->getConn(),me);
   pqxx::ilostream s(w, 137396407);   

   ofstream file("test2.seqb",
ofstream::binary|ios::trunc);
   long size;
   char * buffer(0);
  
   cout << "Streaming blob into a file "
<< endl;
   if(s.good())
   {
      s.seekg (0, pqxx::ilostream::end);  //position stream
pointer to 
end of stream
      size = s.tellg();  //use this to get the size
      cout << "SIZE OF LOSTREAM " <<
size << endl;
      s.seekg(0, pqxx::ilostream::beg);  //reposition back
to beginning
      buffer = new char [size];
      s.read (buffer, size);
      file.write(buffer,size);

      delete[] buffer;
      file.close();
   }



Jeroen T. Vermeulen wrote:
> On Thu, September 6, 2007 05:44, Shannon Allen wrote:
>   
>> Update:
>>
>> Getting closer.  I can now get the size of the
lostream and it matches
>> the size of the file before I put it into the BLOB.
 The problem I'm
>> having now is when I go to write the lostream back
out to a file, in
>> Linux whenever I do a cmp originalfile newfile, it
says that they are
>> different ("differ: byte 2590721, line
35").  But the sizes match
>> exactly.  Anyone have any idea??
>>     
>
> Hmm...  Unwanted encoding conversion maybe?  This stuff
has long been
> flushed out of my mental cache, but maybe the LO stream
classes need to
> add an ios::binary option somewhere.  :-/
>
>
> Jeroen
>
>   

_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general

Re: lostream to a file
user name
2007-09-07 12:09:35
Another question on this.  I am implementing an API for
users where I 
try to hide all of the libpqxx back-end connection work.  So
they 
essentially either call load(stuff) and get(stuff) when
wanting to 
push/get data from the database.  For the purposes of
setting up a 
lostream, what is the best way to implement this?  Should I
create an 
ilostream pointer and pass them the pointer?  I tried doing
this but, it 
seems you can't assign one ilostream object to another
(perhaps for 
obvious reasons that are not obvious to me).  And maybe I
shouldn't even 
try to do this, but I was just trying to make the database
interface as 
simple as possible for user applications.  Thoughts??

ie.

pqxx::ilostream & DBInterface::getBlobStream(int loId)
{
    std::string me = "getBlob";
    pqxx::work w(*_conn,me);
    static pqxx::ilostream loStream(w, loId);
    return loStream;  //really shouldn't return local var,
but just 
testing for now
}

....

calling app
...
   pqxx::ilostream s = db->getBlob(conn, 137396407));   
<== causes 
compiler error  

Thanks.


Jeroen T. Vermeulen wrote:
> On Thu, September 6, 2007 05:44, Shannon Allen wrote:
>   
>> Update:
>>
>> Getting closer.  I can now get the size of the
lostream and it matches
>> the size of the file before I put it into the BLOB.
 The problem I'm
>> having now is when I go to write the lostream back
out to a file, in
>> Linux whenever I do a cmp originalfile newfile, it
says that they are
>> different ("differ: byte 2590721, line
35").  But the sizes match
>> exactly.  Anyone have any idea??
>>     
>
> Hmm...  Unwanted encoding conversion maybe?  This stuff
has long been
> flushed out of my mental cache, but maybe the LO stream
classes need to
> add an ios::binary option somewhere.  :-/
>
>
> Jeroen
>
>   

_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general

Re: lostream to a file
user name
2007-09-08 06:13:14
On Sat, September 8, 2007 00:09, Shannon Allen wrote:
> Another question on this.  I am implementing an API for
users where I
> try to hide all of the libpqxx back-end connection
work.  So they
> essentially either call load(stuff) and get(stuff) when
wanting to
> push/get data from the database.  For the purposes of
setting up a
> lostream, what is the best way to implement this? 
Should I create an
> ilostream pointer and pass them the pointer?  I tried
doing this but, it
> seems you can't assign one ilostream object to another
(perhaps for
> obvious reasons that are not obvious to me).  And maybe
I shouldn't even
> try to do this, but I was just trying to make the
database interface as
> simple as possible for user applications.  Thoughts??

You could allocate the stream on the free store and return
an auto_ptr. 
Few APIs use it, but returning an auto_ptr effectively says
to the caller:
"here, it's on the heap, but you own it now" much
like you can do in
garbage-collected languages.


> pqxx::ilostream & DBInterface::getBlobStream(int
loId)
> {
>     std::string me = "getBlob";
>     pqxx::work w(*_conn,me);
>     static pqxx::ilostream loStream(w, loId);
>     return loStream;  //really shouldn't return local
var, but just
> testing for now
> }
>
> ....
>
> calling app
> ...
>    pqxx::ilostream s = db->getBlob(conn,
137396407));    <== causes
> compiler error

The fact that the function is called getBlobStream(), not
getBlob(), can't
be helping.  


Jeroen


_______________________________________________
Libpqxx-general mailing list
Libpqxx-generalgborg.postgresql.org
http://gborg.postgresql.org/mailman/listinfo/libpqxx
-general

[1-9]

about | contact  Other archives ( Real Estate discussion Medical topics )