public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Fgets is not working on linux/gcc3.2.1
@ 2003-04-10 12:34 Ajay Bansal
  2003-04-10 13:52 ` Bharathi S
  0 siblings, 1 reply; 8+ messages in thread
From: Ajay Bansal @ 2003-04-10 12:34 UTC (permalink / raw)
  To: John Love-Jensen, redhat-devel-list, gcc-help

Problem was not that I _have_ to read a file somehow. Problem was.. How
do read using fgets.  & why it bbehaves differently on Solaris/Linux
with same compiler. 

If I open the file with "a+", the on Solaris file pointer is at the
beginning of the file whereas on Linux it is at the end of the file. Why
is this difference???

And if some code is already written using fgets on solaris, this
difference makes it non-portable!!!!!


-----Original Message-----
From: John Love-Jensen [mailto:eljay@adobe.com] 
Sent: Thursday, April 10, 2003 5:56 PM
To: Ajay Bansal; redhat-devel-list@redhat.com; gcc-help@gcc.gnu.org

Hi Ajay,

Why not do it C++ style, instead of co-mingling C and C++ styles?

Sincerely,
--Eljay

PS:  your data-type Hungarian notation is incorrect.  I strongly suggest
that you avoid date-type Hungarian notation, unless you are going to be
meticulously accurate.  (Otherwise it contributes to code obfuscation.)

PPS:  I also recommend on "using" the particular item in the other
namespace, instead of pulling in the whole namespace.

PPPS:  I also recommend using C++ headers, instead of pulling in C
headers into your C++ code.  (i.e., <cerrno> and <cstdlib>.)

---------------------

#include <iostream>
  using std::cout;
  using std::endl;
#include <fstream>
  using std::
#include <string>
  using std::string;
// #include <cerrno> -- not needed.
// #include <cstdlib> -- not needed.
int main()
{
    string fileName("/tmp/log.txt");
    string line;
    ifstream file(fileName.c_str());
    int lineNumber = 0;

    while (getline(file, line)) {
       ++lineNumber;
       cout << "Line " << lineNumber << " read is : " << line << endl;
    }

    return 0;
}


^ permalink raw reply	[flat|nested] 8+ messages in thread
* RE: Fgets is not working on linux/gcc3.2.1
@ 2003-04-10  7:03 Ajay Bansal
  2003-04-10  8:06 ` Bharathi S
  0 siblings, 1 reply; 8+ messages in thread
From: Ajay Bansal @ 2003-04-10  7:03 UTC (permalink / raw)
  To: Bharathi S, GNU GCC

But then, would the behavior be different on Soalris/Linux!!!!!!!!! 


-----Original Message-----
From: Bharathi S [mailto:bharathi@lantana.tenet.res.in] 
Sent: Thursday, April 10, 2003 12:15 PM
To: GNU GCC

On Thu, 10 Apr 2003, 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.
> Instead it breals after it encounters fgets for the first time.

File is opened in Append mode. Bcoz of this file pointer may positioned
in the end of file. 

Try it by opening in Read mode.

HTH :)
--
Bharathi S, IndLinuX Team,  (__)
DON Lab,      TeNeT Group,  oo )
IIT-Madras, Chennai-INDIA.  (_/\


^ permalink raw reply	[flat|nested] 8+ messages in thread
* Fgets is not working on linux/gcc3.2.1
@ 2003-04-10  6:30 Ajay Bansal
  2003-04-10  6:45 ` Bharathi S
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Ajay Bansal @ 2003-04-10  6:30 UTC (permalink / raw)
  To: redhat-devel-list, gcc-help

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

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2003-04-10 14:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-10 12:34 Fgets is not working on linux/gcc3.2.1 Ajay Bansal
2003-04-10 13:52 ` Bharathi S
  -- strict thread matches above, loose matches on Subject: below --
2003-04-10  7:03 Ajay Bansal
2003-04-10  8:06 ` Bharathi S
2003-04-10  6:30 Ajay Bansal
2003-04-10  6:45 ` Bharathi S
2003-04-10 12:26 ` John Love-Jensen
2003-04-10 14:45 ` LLeweLLyn Reese

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).