From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2533 invoked by alias); 10 Apr 2003 14:45:33 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 2526 invoked from network); 10 Apr 2003 14:45:32 -0000 Received: from unknown (HELO lifesupport.shutdown.com) (66.93.79.177) by sources.redhat.com with SMTP; 10 Apr 2003 14:45:32 -0000 Received: (from llewelly@localhost) by lifesupport.shutdown.com (8.11.2/8.11.2) id h3AEgDF07108; Thu, 10 Apr 2003 07:42:14 -0700 (PDT) To: "Ajay Bansal" Cc: , Subject: Re: Fgets is not working on linux/gcc3.2.1 Reply-To: gcc-help@gcc.gnu.org References: <2B721C6525F0D411B1E900B0D0226BDD027DCC6D@mohmsg01.ad.infosys.com> From: LLeweLLyn Reese Date: Thu, 10 Apr 2003 14:45:00 -0000 In-Reply-To: <2B721C6525F0D411B1E900B0D0226BDD027DCC6D@mohmsg01.ad.infosys.com> Message-ID: User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-SW-Source: 2003-04/txt/msg00126.txt.bz2 "Ajay Bansal" writes: > Hi All > > Following program is not working on linux. Ideally it should read all > the lines from "/tmp/log.txt" and print them on to the screen. Instead > it breals after it encounters fgets for the first time. > > Pleas tell me where I am going wrong!!!! This program is working as > expected on a Solaris machine. > > Regards > -Ajay > ------------------------------------------------------------------------ > ----------- > > Entries in log.txt > ------------------ > Line1 > Line2 > Line3 > ------------------ > > Output of the program > --------------------- > [ajay@linux1 test]$ ./a.out > Fileno is :3 > Error no is : 0 > Error is : Success > Line read is : > after fgets break outside while loop > --------------------- > > #include > #include > #include > #include > using namespace std; > int main() > { > char * pszFileName = "/tmp/log.txt"; > char * pszMode = "a+"; > char lpsz[4096]; > FILE *pFile = fopen(pszFileName, pszMode); fseek(pFile,0,SEEK_BEG); will fix your problem. On solaris, where a file opened with a is opened at the begining, the above will have no effect. On linux, where the file is opened at the end, the above will seek to the begining. > > while (!feof(pFile)) { > if (! fgets(lpsz, 4096, pFile)) > { > cout<<"Error no is : "< cout<<"Error is : "< cout<<"Line read is : "< break; > } > cout<<"Line read is : "< cout <<" after fgets break inside while loop"< } > > cout <<" after fgets break outside while loop"< > return 1; > }