This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Fgets is not working on linux/gcc3.2.1
- From: LLeweLLyn Reese <llewelly at lifesupport dot shutdown dot com>
- To: "Ajay Bansal" <Ajay_Bansal at infosys dot com>
- Cc: <redhat-devel-list at redhat dot com>, <gcc-help at gcc dot gnu dot org>
- Date: 10 Apr 2003 07:42:12 -0700
- Subject: Re: Fgets is not working on linux/gcc3.2.1
- References: <2B721C6525F0D411B1E900B0D0226BDD027DCC6D@mohmsg01.ad.infosys.com>
- Reply-to: gcc-help at gcc dot gnu dot org
"Ajay Bansal" <Ajay_Bansal at infosys dot com> 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 at linux1 test]$ ./a.out
> Fileno is :3
> Error no is : 0
> Error is : Success
> Line read is :
> after fgets break outside while loop
> ---------------------
>
> #include <iostream>
> #include <fstream>
> #include <errno.h>
> #include <stdlib.h>
> 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 : "<<errno<<endl;
> cout<<"Error is : "<<strerror(errno)<<endl;
> cout<<"Line read is : "<<lpsz<<endl;
> break;
> }
> cout<<"Line read is : "<<lpsz<<endl;
> cout <<" after fgets break inside while loop"<<endl;
> }
>
> cout <<" after fgets break outside while loop"<<endl;
>
> return 1;
> }