delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2010/04/01/06:27:22

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW,T_RP_MATCHES_RCVD
X-Spam-Check-By: sourceware.org
Message-ID: <4BB482FA.3030201@ltu.se>
Date: Thu, 01 Apr 2010 13:26:50 +0200
From: Bengt-Arne Fjellner <Bengt-Arne DOT Fjellner AT ltu DOT se>
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: fopen with "a+" does not respect setting file read position to start of file
References: <B0E0B6C348D8AB4BAD6290DC0BDE99D47427D7442A AT GVW1115EXC DOT americas DOT hpqcorp DOT net>
In-Reply-To: <B0E0B6C348D8AB4BAD6290DC0BDE99D47427D7442A@GVW1115EXC.americas.hpqcorp.net>
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

On 2010-04-01 11:06 AM, Brebner, Gavin wrote:
> We hit a problem in a code, and narrowed it down to a test case that does :
>
>    while (cnt) {
>      f=fopen(host_file, "a+");
>      if (!f)
>        perror("dup_host_file: Could not open hostfile");
>      /* rewind(f); */
>      while (cnt) {
>        int ret=fscanf(f, "%s", line);
>        if (ret != EOF) {
> 	  fprintf(f, "%s\n", line);
> 	  cnt--;
> 	  dup++;
> 	}
>      }
>      fclose(f);
>
> In earlier versions of cygwin we have, this works fine, however in the recently installed
> versions, it no longer works. It seems that fopen(host_file, "a+") is NOT positioning the
> read position at the start of the file as it should. Adding an explicit rewind(f) is a
> work around.
>    
Well if that worked previously it was a bug.

 From the manual for fopen:
  ``a+''  Open for reading and writing.  The file is created if it does 
not exist.  The stream is positioned at the
              end of the file.  Subsequent writes to the file will 
always end up at the then current end of file, irre-
              spective of any intervening fseek(3) or similar.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


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