public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* (1) Multiple  Stdio.h  files - which one?  (2) fgets()
@ 2011-12-28 20:46 Matthew D. Gutchess
  2011-12-28 20:53 ` Jonathan Wakely
  2011-12-29  6:26 ` Jonathan Wakely
  0 siblings, 2 replies; 3+ messages in thread
From: Matthew D. Gutchess @ 2011-12-28 20:46 UTC (permalink / raw)
  To: gcc-help

Hi, 
    I am running Ubuntu-64 version 11.04 and teaching myself C++ by modifying, compiling and running sample programs.
    (1) Ubuntu / Unix came with multiple, different stdio.h files.  There are no manual pages for stdio.h.  Without specifying otherwise the first stdio.h will be used, correct?  When should the other various versions be used?

matt@ComputaBeauta15:~/C-Programs/EXAMPLES$ locate /stdio.h
/usr/include/stdio.h
/usr/include/bits/stdio.h
/usr/include/c++/4.5/tr1/stdio.h
/usr/lib/syslinux/com32/include/stdio.h

matt@ComputaBeauta15:~/C-Programs/EXAMPLES$ wc /usr/include/stdio.h
  940  4406 31525 /usr/include/stdio.h

matt@ComputaBeauta15:~/C-Programs/EXAMPLES$ wc /usr/include/bits/stdio.h
 191  797 5654 /usr/include/bits/stdio.h

matt@ComputaBeauta15:~/C-Programs/EXAMPLES$ wc /usr/include/c++/4.5/tr1/stdio.h
  34  202 1210 /usr/include/c++/4.5/tr1/stdio.h

matt@ComputaBeauta15:/usr/include$ wc /usr/lib/syslinux/com32/include/stdio.h
 117  472 3129 /usr/lib/syslinux/com32/include/stdio.h



    (2) In the sample program below, using fgets() instead of gets() results in a "No such file or directory error", but the program would open and display the file when gets() was used.  Is this because fgets() appends "/0" to the entered file name and this must be stripped off?

/*

 * L I S T _ 3 version 4

 *

 * Display the contents of an ASCII text file

 * file on the user's screen.

 */



#include <stdio.h>

#include <errno.h>

#include <string.h>

#define MAXPATH 64

#define MAXLINE 256



int

main(void)

{

	int ch;                 /* input character */

	FILE *fp;               /* file pointer */

	char pathname[MAXPATH]; /* filename buffer */

	char line[MAXLINE];     /* line buffer for fgets() */

	int name_length;        /* length of file name */



	printf("Read and write a file \n");

	/*

	 * Prompt the user for a filename and read it.

	 */



        printf("Filename: ");

	fgets(pathname, sizeof(pathname), stdin); /* instead of gets(pathname) */
	printf("You entered %s ", pathname);
	name_length = strlen(pathname) - 1;       
        /* subtract 1 to not count terminating \0 */



	if (pathname == '\0') 

           {

            printf("\n\n File name is null. \n");

	    return (0);

           }

	if (name_length == 0)

           {

            printf("\n\n File not named. \n");

	    return (0);

           }

        else

           {

   	   printf("\n Opening %s Name length= %d \n", pathname,  name_length);

  	   /*

	    * Open the named file for reading.

	    */

 	   fp = fopen(pathname, "r"); 

           int err_open = errno;     /* save global variable errno from fopen() */

	   /* perror(strerror(err_open));     */

 	   if (err_open != 0) 

	     {

             printf("\n OPEN failed, errno= %d, %s\n\n", err_open, strerror(err_open));

             }

            else

               	{

               	 printf("\n Opened %s ", pathname, "\n");

	       	 /*

	       	 * Read the contents of the file and display it

	       	 * one line at a time as it is read.

	       	 */

    	       	 while (fgets(line, MAXLINE, fp) != NULL)

		     fputs(line, stdout);


	         /*

	          * Close the file.

	          */

	         fclose(fp);

	        } 

           }

        

	return (0);

}


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

* Re: (1) Multiple Stdio.h files - which one? (2) fgets()
  2011-12-28 20:46 (1) Multiple Stdio.h files - which one? (2) fgets() Matthew D. Gutchess
@ 2011-12-28 20:53 ` Jonathan Wakely
  2011-12-29  6:26 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2011-12-28 20:53 UTC (permalink / raw)
  To: Matthew D. Gutchess; +Cc: gcc-help

On 28 December 2011 20:23, Matthew D. Gutchess wrote:
> Hi,
>    I am running Ubuntu-64 version 11.04 and teaching myself C++ by modifying, compiling and running sample programs.
>    (1) Ubuntu / Unix came with multiple, different stdio.h files.  There are no manual pages for stdio.h.  Without specifying otherwise the first stdio.h will be used, correct?  When should the other various versions be used?

> matt@ComputaBeauta15:~/C-Programs/EXAMPLES$ locate /stdio.h
> /usr/include/stdio.h

This is what gets included when you say #include <stdio.h>

> /usr/include/bits/stdio.h

This is probably included by <stdio.h>, you should not include it
yourself, it's an implementation detail not meant for you to know or
care about.

> /usr/include/c++/4.5/tr1/stdio.h

That will be used if you #include <tr1/stdio.h>

TR1 is a report by the ISO C++ committee defining a set of extensions
to the C++ library. It specified additions to <stdio.h>, for GCC's
implementation of TR1 you need to include <tr1/stdio.h> to use those
extensions, which will include <stdio.h> and also define the
extensions.  Unless you want to use the TR1 features you don't need to
care about this either.

> /usr/lib/syslinux/com32/include/stdio.h

This is a Linux kernel header, used when building kernel modules which
need <stdio.h> but can't rely on the C library being present. You
probably don't need to know about it.


In short, all you need to do is just #include <stdio.h>, which will do
the right thing, which in your case is to include /usr/include/stdio.h

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

* Re: (1) Multiple Stdio.h files - which one? (2) fgets()
  2011-12-28 20:46 (1) Multiple Stdio.h files - which one? (2) fgets() Matthew D. Gutchess
  2011-12-28 20:53 ` Jonathan Wakely
@ 2011-12-29  6:26 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2011-12-29  6:26 UTC (permalink / raw)
  To: Matthew D. Gutchess; +Cc: gcc-help

On 28 December 2011 20:23, Matthew D. Gutchess wrote:
>    (2) In the sample program below, using fgets() instead of gets() results in a "No such file or directory error", but the program would open and display the file when gets() was used.  Is this because fgets() appends "/0" to the entered file name and this must be stripped off?

No, the '\0' added by fgets is necessary.

Change the line that prints out the filename like so and you should
see the problem:

       printf("You entered '%s' ", pathname);

If that doesn't help, read the man page for fgets which is quite clear
about its behaviour.

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

end of thread, other threads:[~2011-12-28 20:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-28 20:46 (1) Multiple Stdio.h files - which one? (2) fgets() Matthew D. Gutchess
2011-12-28 20:53 ` Jonathan Wakely
2011-12-29  6:26 ` Jonathan Wakely

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