X-Recipient: archive-cygwin AT delorie DOT com X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes To: cygwin AT cygwin DOT com Subject: Re: fopen with "a+" does not respect setting file read position to start of file References: <4BB482FA DOT 3030201 AT ltu DOT se> Date: Thu, 01 Apr 2010 14:53:38 +0300 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Matthias Andree" Message-ID: In-Reply-To: <4BB482FA.3030201@ltu.se> User-Agent: Opera Mail/10.51 (Win32) X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Bengt-Arne Fjellner wrote on 2010-04-01: > 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. Not a Cygwin defect, but an application bug, and what Gavin named "work around" is actually the right fix. POSIX explicitly states that the initial position beginning/end of file in append mode is implementation-defined, and the Cygwin documentation doesn't make promises either way. Linux and FreeBSD document BOF and EOF, respectively. > 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. Irrelevant, as that is neither from POSIX nor from cygwin-doc-1.5-1. -- Matthias Andree -- 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