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: "Ajay Bansal" <Ajay_Bansal at infosys dot com>
- To: "John Love-Jensen" <eljay at adobe dot com>,<redhat-devel-list at redhat dot com>,<gcc-help at gcc dot gnu dot org>
- Date: Thu, 10 Apr 2003 18:03:02 +0530
- Subject: RE: Fgets is not working on linux/gcc3.2.1
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 at adobe dot com]
Sent: Thursday, April 10, 2003 5:56 PM
To: Ajay Bansal; redhat-devel-list at redhat dot com; gcc-help at gcc dot gnu dot 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;
}