(1) Multiple Stdio.h files - which one? (2) fgets()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

}





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux