I would make your routine more like this. It avoids the needless buffer shadowing you get by using fopen and also avoids unecessary mallocs.

I haven't tested the code, it just presents the method.

  unsigned char *bp;
  int           fd;
  long          fsize;

  fd = open(path, O_RDWR);          /*Get handle to file.*/
  if (fd < 0) return E_IO_COULDNT_OPEN;

  fsize = lseek(fd, 0, SEEK_END);   /*Gives file size.*/

  madvise(bp, fsize, MADV_SEQUENTIAL);  /*May make it run better.*/

  /*Map file into local addressing space.*/
  bp = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd,  0);

  close(fd);   /*Don't need the handle noe.*/

  /*Place bind of bp to BLOB here usinjg sqlite3_bind_blob.*/
  /*!!!!*/

  /*Execute SQL here using sqlite3_step.*/
  /*!!!!*/

  /*Reset sql statement.*/
  /*!!!!*/

  unmap(bp);     /*Release file from local addressing space.*/

>
> uchar * value;
> char *query;
> long len;
>
> if ( LoadFileContent("file.jpeg",&value, &len) != 0)
> exit();
>
> query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value); /* here i
> have a segmentation fault! */
>
Cesar Rodas wrote:
#define E_IO_COULDNT_OPEN - 1
#define E_IO_ERRO -2
#define uchar unsigned char *

int LoadFileContent(uchar *path, uchar ** value, long * len)
{
   #define Buffsize 1024*4

   int n;
   long i=0;
   FILE *file;
   unsigned char buff[Buffsize];

   if ( (file=fopen(path,"rb"))==0)
   {
       return E_IO_COULDNT_OPEN;
   }

   *value = malloc(Buffsize);
   *len = 0;
   fseek(file,0L,SEEK_SET);
   while ( (n=fread(buff,Buffsize,1,file)) > 0)
   {
       if (i>0)
           *value = realloc(*value, (i+1) * Buffsize);
       memcpy(*value + (i * Buffsize), buff,  Buffsize);
       *len += n;
       i++;
   }
   if (n==-1)
   {
       fclose(file);
       return E_IO_ERRO;
   }

   fclose(file);
   return 0;
}


uchar * value;
char *query;
long len;

if ( LoadFileContent("file.jpeg",&value, &len) != 0)
exit();

query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value); /* here i
have a segmentation fault! */

/****************** Here I compile in debug mode there segmentation fault is
here *****/
sqlite3/printf.c LINE 608

for(i=n=0; (ch=escarg[i])!=0; i++){
escarg is NULL;

I know that is not an SQLite bug. I know that that is my fault.

Thanks.





-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to