linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: _z33 <timid.Gentoo@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: Confusing Prototype
Date: Mon, 12 Sep 2005 18:00:22 +0530	[thread overview]
Message-ID: <dg3sd3$cd7$1@sea.gmane.org> (raw)
In-Reply-To: <432470B7.7050609@colannino.org>

James Colannino wrote:
> Hey everyone.  I was looking at the prototype for fgets and noticed the 
> following:
> 
> char *fgets(char *s, int size, FILE *stream);
> 
> I'm confused because it says that it returns a pointer to a character, 
> but at the same time it stores a string at the address pointed to by 
> char *s, so why does it also need to return a pointer?  I'm confused.  

   please correct me if I'm wrong. As far as I know, you need a 
mechanism to find out if fgets, executed perfectly or not, and that's 
the primary reason it returns a value. When fgets fails to read from the 
stream specified it returns a null pointer to indicate the error. The 
string in the argument list of the function however, will have the value 
till which execution proceeded without error. (hope I'm right about this)

   If in case the returning pointer, was avoided, then the error 
couldn't be properly indicated. And the only way will be to modify the 
input string container itself. This could create an ambiguity, if the 
input file or stream had nothing but a null value. The programmer might 
not be able to differentiate whether an error occured or whether input 
was null.

#include <stdio.h>

int main (int argc, char *argv[])
{
     /* for file access */
     FILE *fp;

     /* array to store some 5 characters */
     char array[5] = {'\0'};

     /* to store the return value */
     char *status;

     /* open the file for reading */
     if ((fp = fopen ("testFile.txt", "r")) == NULL)
     {
         /* display error information and exit */
         fprintf (stderr, "Error: file read failed. \n");
         exit (1);
     }

     /* read twice */
     status = fgets (array, sizeof (array), fp);
     status = fgets (array, sizeof (array), fp);

     /*
      * without the return value fgets can't display the error
      * encountered.
      * the input string passed is never touched when an error occurs.
      */

     /* close file */
     fclose (fp);

     /* display the array value */
     printf ("array: %s \n", array);

     /* display the status returned */
     printf ("status: %s \n", status);

     return (0);
}

Sorry, if I have confused you.

_z33
-- 
I love TUX; well... that's an understatement :)


  reply	other threads:[~2005-09-12 12:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-11 18:00 Confusing Prototype James Colannino
2005-09-12 12:30 ` _z33 [this message]
2005-09-12 16:36   ` Steve Graegert
2005-09-13  3:56     ` _z33
2005-09-13 18:34       ` Steve Graegert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='dg3sd3$cd7$1@sea.gmane.org' \
    --to=timid.gentoo@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).