[GRASS-dev] Re: [GRASS GIS] #1006: r.terraflow fails to stat() stream file on Windows

GRASS GIS trac at osgeo.org
Thu Apr 8 23:30:48 EDT 2010


#1006: r.terraflow fails to stat() stream file on Windows
------------------------------+---------------------------------------------
  Reporter:  marisn           |       Owner:  grass-dev at lists.osgeo.org 
      Type:  defect           |      Status:  new                       
  Priority:  critical         |   Milestone:  6.4.0                     
 Component:  Raster           |     Version:  svn-releasebranch64       
Resolution:                   |    Keywords:  wxgui, wingrass, terraflow
  Platform:  MSWindows Vista  |         Cpu:  x86-32                    
------------------------------+---------------------------------------------
Comment (by glynn):

 Replying to [comment:14 hamish]:

 > is stat() on Windows the same as it is on UNIX?

 Sort of. '''However''': if the file is open, it reports the size as zero
 bytes (or possibly the original size if the file was opened for update
 without truncation). Once the file is closed, it reports the correct size.
 Also, fstat() works correctly on open files.

 Test case:
 {{{
 $ cat test.c
 #include <stdio.h>
 #include <sys/stat.h>

 int main(void)
 {
         char buf[1024];
         struct stat st;
         FILE *fp;
         int i;
         fp = fopen("test.dat", "wb");
         for (i = 0; i < 64; i++)
                 fwrite(buf, 1, sizeof(buf), fp);
         fflush(fp);
         stat("test.dat", &st);
         printf("%d\n", (int)st.st_size);
         fstat(fileno(fp), &st);
         printf("%d\n", (int)st.st_size);
         fclose(fp);
         stat("test.dat", &st);
         printf("%d\n", (int)st.st_size);
         return 0;
 }

 $ gcc -Wall test.c
 $ ./a.exe
 0
 65536
 65536
 }}}

 A more portable way to determine the length of a file is to use
 fseek(SEEK_END), e.g.:
 {{{
         long save = ftell(fp);
         fseek(fp, 0, SEEK_END);
         long size = ftell(fp);
         fseek(fp, save, SEEK_SET);
         return size;
 }}}

-- 
Ticket URL: <http://trac.osgeo.org/grass/ticket/1006#comment:22>
GRASS GIS <http://grass.osgeo.org>


More information about the grass-dev mailing list