delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/30/17:35:44

From: DavMac AT iname DOT com (Davin McCall)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Bug in ftruncate or not?
Date: Fri, 30 Jul 1999 08:15:15 GMT
Organization: Monash Uni
Lines: 80
Distribution: world
Message-ID: <37a15d60.24986840@newsserver.cc.monash.edu.au>
References: <7noh88$a01$1 AT newssrv DOT otenet DOT gr>
NNTP-Posting-Host: damcc5.halls.monash.edu.au
X-Trace: towncrier.cc.monash.edu.au 933322465 7888 130.194.198.138 (30 Jul 1999 08:14:25 GMT)
X-Complaints-To: abuse AT monash DOT edu DOT au
NNTP-Posting-Date: 30 Jul 1999 08:14:25 GMT
X-Newsreader: Forte Free Agent 1.1/32.230
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Inserting "fflush(f)" immediately after the "fwrite" line solves the
problem. It is not that "ftruncate" is buggy, only that it works on
the unbuffered level. That is (without fflush, '#define buggy'
uncommented), your program:

-writes 500 bytes, starting at position 500 into a buffer (not written
to file)
-The "ftruncate" call chops the physical size of the file to 500
bytes;
-Your program finishes, at which point buffers are flushed. In this
case, the 500 buffered bytes are written at position 500, thereby
increasing the file size to 1000.

Davin.



On Thu, 29 Jul 1999 06:18:21 +0300, "Pavlos" <trash24379 AT usa DOT net>
wrote:

>Hello,
>I know that most of 'bug' reports are actually a fault of the user... Do I
>do something wrong here?
>
>This simple program will truncate the file only if you comment out #define
>BUGGY.
>At least in my PC...
>If you don't comment #define BUGGY, it won't truncate the file.
>Here is the code.
>When you run it, press '1' the first time to create the file. Then run it
>again and press '2' to truncate it.
>Thank you.
>
>Pavlos
>
><--- cut here --->
>
>#include <stdio.h>
>#include <stdlib.h>
>
>#define BUGGY
>
>int main(int argc, char **argv)
>{
>   FILE *f;
>   char buf[1000];
>
>   printf("1: Create file (1000 bytes), 2:Truncate file (to 500)\n");
>   printf("Your choice: ");
>
>   switch(getch())
>   {
>      case  '1':
>         f = fopen("TEST1234.$$$", "wb+");
>         fwrite(buf, 1000, 1, f);
>         return(0);
>
>      case  '2':
>         f = fopen("TEST1234.$$$", "rb+");
>         fread(buf, 1000, 1, f);
>         #ifdef BUGGY
>         fseek(f, 500, SEEK_SET);
>         fwrite(buf, 500, 1, f);
>         #endif
>         ftruncate(fileno(f), 500);
>         return(0);
>
>      default:
>         return(0);
>   }
>}
>
><--- cut here --->
>
>
>

__________________________________________________________
       *** davmac - sharkin'!! davmac AT iname DOT com ***
my programming page: http://yoyo.cc.monash.edu.au/~davmac/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019