473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

return value of fgets()

Hello, All!

I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Dec 16 '05 #1
6 5387
Roman Mashak wrote:
while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?


There is no reason. This code is silly. Just use NULL.

Also, there is no need for parentheses around buf in `sizeof buf'. Using
parentheses makes one think that it is a type whose size is being
tested, but in this case it is just an object.

--
Simon.
Dec 16 '05 #2
"Roman Mashak" <mr*@tusur.ru> writes:
I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?


There's no good reason to use (char*)0 rather than NULL. On the other
hand, the effect is the same; NULL is just (IMHO) simpler and better
style.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 16 '05 #3
Roman Mashak wrote:
Hello, All!

I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru


Just bad taste. Assuming there is in scope 'char buf[N];'..

while (fgets(buf, sizeof buf, fp)) {}

...is the Wright way. The forms..

while (fgets(buf, sizeof buf, fp) != NULL) {}

while (fgets(buf, sizeof buf, fp) != 0) {}

...are acceptable of course.

Note that sizeof is an operator, not a function. It's argument is a
object's name or a type. If an object, no parentheses are necessary. If
a type, the Wright way is 'sizeof (long)' with a space before the '(' so
that it doesn't look so much like a function.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 16 '05 #4
Roman Mashak wrote:

I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to
char pointer) rather than simply with NULL?


None. It would be clearer with just NULL, or even 0. To my mind
there is no reason for a comparison at all (others will disagree).

while (fgets(buf, sizeof(buf), fp)) {
dostuff();
}

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 16 '05 #5
"Chuck F. " <cb********@yahoo.com> writes:
Roman Mashak wrote:
I met a source code where
while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )
What's the special reason to compare result with 0 (casting to char
pointer) rather than simply with NULL?


None. It would be clearer with just NULL, or even 0. To my mind
there is no reason for a comparison at all (others will disagree).

while (fgets(buf, sizeof(buf), fp)) {
dostuff();
}


Just to prove that you're right, I prefer an explicit comparison:

while (fgets(buf, sizeof(buf), fp) != NULL) {
dostuff();
}

But of course any C programmer needs to be able to read code using
either convention (and if you're maintaining existing code, you should
probably conform to the existing style unless it's horrendously bad).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 16 '05 #6
Keith Thompson wrote:

"Chuck F. " <cb********@yahoo.com> writes:
Roman Mashak wrote:
I met a source code where
while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )
What's the special reason to compare result with 0 (casting to char
pointer) rather than simply with NULL?


None. It would be clearer with just NULL, or even 0. To my mind
there is no reason for a comparison at all (others will disagree).

while (fgets(buf, sizeof(buf), fp)) {
dostuff();
}


Just to prove that you're right, I prefer an explicit comparison:

while (fgets(buf, sizeof(buf), fp) != NULL) {
dostuff();
}


There's also:

while (fgets(buf, sizeof buf, fp) == buf) {
dostuff();
}

--
pete
Dec 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tanel | last post by:
Hello, I need to read a result of the first script (that takes some time to run) from the second script so that the first script doesn't block the second. Here is a short example. do_smth_else()...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
17
by: Yogi_Bear_79 | last post by:
I have the following code: sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0; It only partially works. If the user types a character other than 0-9 to start the string it fails. However as long as...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
12
by: spammenotplui31 | last post by:
I have a program that needs to be compiled as 64bit. I have another program that needs to be compiled as 32bit and will call that 64-bit binary. Is there a way for the 64-bit binary to return a...
7
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
1
by: jef.d | last post by:
I am attempting to read through a text file & then update an HTML page table w/ the output from the text file (ie; statusing by table). What I want the code to do is read through the file, look...
23
by: Bill Cunningham | last post by:
I have been using fgetc and fputc with a while loop construct to copy files. It seems to work fine but the thing is I always check for fgetc's RV and it's always -1. If the program doesn't work...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.