Question on ? "$" :

Posted by Azrith on Tue 04 Feb 2003 10:30 PM — 5 posts, 20,832 views.

#0
Hey everyone!
I was wondering if someone could give me a little more info as to what exactly the ? "$": does in the line
filename = feof( fpList ) ? "$" : fread_word( fpList );

This line is with
filename = feof( fpList ) ? "$" : fread_word( fpList );
if ( filename[0] == '$' )
break;

Thanks!

Kyle
#1
PS :)

I realize the "$" is the end of file so is it saying if $ is found then filename = EOF otherwise filename = fread_word?

Thanks!
Australia Forum Administrator #2
What they are saying is that a file *should* end with "$" but if it doesn't, in other words, if feof is true, then simulate that by putting a "$" into filename, otherwise read a filename from the file.

Then the subsequent test just tests for a "$" and doesn't have to test for end-of-file as well.
Australia #3
The ? and : thing is called the ternary operator, and it's a short-hand form of a simple if-then-else. For example:
filename = feof(fpList) ? "$" : fread_word(fpList);
Could be written as:
if (feof(fpList))
    filename = "$";
else
    filename = fread_word(fpList);
Like many things in C, the ternary operator can be abused. I've seen massively-nested ?:'s which, while amusing, were certainly unreadable. For more information, google for "ternary operator".

Hope this helps,

Dave
Amended on Fri 07 Feb 2003 10:13 PM by Dave
#4
Thanks for all the help everyone :)
Ya I remember going over this a long time ago I had just forgotten.

Thanks again!