Re: Fgets is not working on linux/gcc3.2.1

Michael Schwendt <[email protected]>
Newsgroups gmane.linux.redhat.devel
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, 10 Apr 2003 12:01:24 +0530, Ajay Bansal wrote:

> 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.

The given source code example does not match this description.

Apart from that it would not even compile, because you forgot
to include a few headers.

> 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.

I doubt that. See below.

> 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 <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);

Due to pszMode above, this opens the file at the end.
Therefore it doesn't read the contents of the file.

>     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;
> }

- -- 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE+lSRk0iMVcrivHFQRAllEAJ0U2eg6IHkzYbQhxgn0OFrx33Qi1gCeMqfu
jFzgzvVBpOq2oi2J+nRIdkY=
=IfMm
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.