2.6 Program Examples

This section gives some program examples that show how the I/O functions can be used in applications.

Example 2-1 shows the printf function.

Example 2-1 Output of the Conversion Specifications

/*  This program uses the printf function to print the         *
 *  various conversion specifications and their affect on the  *
 *  output.                                                    */

/*  Include the proper header   *
 *  files in case printf has to *
 *  return EOF.                 */

#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>

#define WIDE_STR_SIZE 20

main()

{
   double   val  = 123345.5;
   char     c    = 'C';
   int      i    = -1500000000;
   char   *s    = "thomasina";
   wchar_t  wc;
   wchar_t  ws[WIDE_STR_SIZE];

/*  Produce a wide character and a wide character string      */

   if (mbtowc(&wc, "W", 1) == -1) {
        perror("mbtowc");
        exit(EXIT_FAILURE);
   }

   if (mbstowcs(ws, "THOMASINA", WIDE_STR_SIZE) == -1) {
        perror("mbstowcs");
        exit(EXIT_FAILURE);
   }

/*  Print the specification code, a colon, two tabs, and the   *
 *  formatted output value delimited by the angle bracket      *
 *  characters (<>).                                           */

   printf("%%9.4f:\t\t<%9.4f>\n", val);
   printf("%%9f:\t\t<%9f>\n", val);
   printf("%%9.0f:\t\t<%9.0f>\n", val);
   printf("%%-9.0f:\t\t<%-9.0f>\n\n", val);

   printf("%%11.6e:\t\t<%11.6e>\n", val);
   printf("%%11e:\t\t<%11e>\n", val);
   printf("%%11.0e:\t\t<%11.0e>\n", val);
   printf("%%-11.0e:\t\t<%-11.0e>\n\n", val);

   printf("%%11g:\t\t<%11g>\n", val);
   printf("%%9g:\t\t<%9g>\n\n", val);

   printf("%%d:\t\t<%d>\n", c);
   printf("%%c:\t\t<%c>\n", c);
   printf("%%o:\t\t<%o>\n", c);
   printf("%%x:\t\t<%x>\n\n", c);

   printf("%%d:\t\t<%d>\n", i);
   printf("%%u:\t\t<%u>\n", i);
   printf("%%x:\t\t<%x>\n\n", i);

   printf("%%s:\t\t<%s>\n", s);
   printf("%%-9.6s:\t\t<%-9.6s>\n", s);
   printf("%%-*.*s:\t\t<%-*.*s>\n", 9, 5, s);
   printf("%%6.0s:\t\t<%6.0s>\n\n", s);

   printf("%%C:\t\t<%C>\n", wc);
   printf("%%S:\t\t<%S>\n", ws);
   printf("%%-9.6S:\t\t<%-9.6S>\n", ws);
   printf("%%-*.*S:\t\t<%-*.*S>\n", 9, 5, ws);
   printf("%%6.0S:\t\t<%6.0S>\n\n", ws);

}

Running Example 2-1 produces the following output:

$ RUN  EXAMPLE
%9.4f:  <123345.5000>
%9f:  <123345.500000>
%9.0f:  <   123346>
%-9.0f:  <123346   >

%11.6e:  <1.233455e+05>
%11e:  <1.233455e+05>
%11.0e:  <      1e+05>
%-11.0e:  <1e+05      >

%11g:  <     123346>
%9g:  <   123346>

%d:  <67>
%c:  <C>
%o:  <103>
%x:  <43>

%d:  <-1500000000>
%u:  <2794967296>
%x:  <a697d100>

%s:  <thomasina>
%-9.6s:  <thomas   >
%-*.*s:  <thoma    >
%6.0s:  <      >

%C:  <W>
%S:  <THOMASINA>
%-9.6S:  <THOMAS   >
%-*.*S:  <THOMA    >
%6.0S:  <      >
$

Example 2-2 shows the use of the fopen, ftell, sprintf, fputs, fseek, fgets, and fclose functions.

Example 2-2 Using the Standard I/O Functions

/*  This program establishes a file pointer, writes lines from *
 *  a buffer to the file, moves the file pointer to the second *
 *  record, copies the record to the buffer, and then prints   *
 *  the buffer to the screen.                                  */

#include <stdio.h>
#include <stdlib.h>

main ()
{
   char   buffer[32];
   int    i, pos;
   FILE   *fptr;
                              /*  Set file pointer.            */
   fptr = fopen("data.dat", "w+");
   if (fptr == NULL)
      {
         perror("fopen");
  exit(EXIT_FAILURE);
      }

   for (i=1; i<5; i++)
      {
         if (i == 2)          /*  Get position of record 2.    */
            pos = ftell(fptr);
                              /*  Print a line to the buffer.  */
         sprintf(buffer, "test data line %d\n", i);
                              /*  Print buffer to the record.  */
         fputs(buffer, fptr);
      }

                              /*  Go to record number 2.       */
   if (fseek(fptr, pos, 0) < 0)
      {
         perror("fseek");     /*  Exit on fseek error.         */
         exit(EXIT_FAILURE);
      }

                              /*  Read record 2 in the buffer. */
   if  (fgets(buffer, 32, fptr) == NULL)
      {
         perror("fgets");     /*  Exit on fgets error.         */
         exit(EXIT_FAILURE);
      }
                              /*  Print the buffer.            */
   printf("Data in record 2 is: %s", buffer);
   fclose(fptr);             /*  Close the file.               */
}

Running Example 2-2 produces the following result:

$ RUN  EXAMPLE
Data in record 2 is: test data line 2

The output to DATA.DAT from Example 2-2 is:

test data line 1
test data line 2
test data line 3
test data line 4

Example 2-3 Using Wide Character I/O Functions

/*  This program establishes a file pointer, writes lines from *
 *  a buffer to the file using wide character codes, moves the *
 *  file pointer to the second record, copies the record to    *
 *  the wide character buffer, and then prints the buffer to   *
 *  the screen.                                                */

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

main ()
{
   char      flat_buffer[32];
   wchar_t   wide_buffer[32];
   wchar_t   format[32];
   int       i, pos;
   FILE      *fptr;
                              /*  Set file pointer.            */
   fptr = fopen("data.dat", "w+");
   if (fptr == NULL)
      {
         perror("fopen");
         exit(EXIT_FAILURE);
      }

   for (i=1; i<5; i++)
      {
         if (i == 2)          /*  Get position of record 2.    */
            pos = ftell(fptr);
                              /*  Print a line to the buffer.  */
         sprintf(flat_buffer, "test data line %d\n", i);
         if (mbstowcs(wide_buffer, flat_buffer, 32) == -1)
           {
           perror("mbstowcs");
           exit(EXIT_FAILURE);
           }

                              /*  Print buffer to the record.  */
         fputws(wide_buffer, fptr);
      }

                              /*  Go to record number 2.       */
   if (fseek(fptr, pos, 0) < 0)
      {
         perror("fseek");     /*  Exit on fseek error.         */
         exit(EXIT_FAILURE);
      }

                              /*  Put record 2 in the buffer.  */
   if   (fgetws(wide_buffer, 32, fptr) == NULL)
      {
         perror("fgetws");     /*  Exit on fgets error.        */
         exit(EXIT_FAILURE);
      }
                              /*  Print the buffer.            */
   printf("Data in record 2 is: %S", wide_buffer);
   fclose(fptr);             /*  Close the file.               */
}

Example 2-4 shows the use of both a file pointer and a file descriptor to access a single file.

Example 2-4 I/O Using File Descriptors and Pointers

/*  The following example creates a file with variable-length  *
 *  records (rfm = var) and the carriage-return attribute      *
 *  (rat = cr).                                                *
 *                                                             *
 *  The program uses creat to create and open the file, and    *
 *  fdopen to associate the file descriptor with a file        *
 *  pointer.  After using the fdopen function, the file        *
 *  must be referenced using the Standard I/O functions.       */

#include <unixio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR 0
#define ERROR1 -1
#define BUFFSIZE 132

main()
{
   char buffer[BUFFSIZE];
   int  fildes;
   FILE *fp;

   if ((fildes = creat("data.dat",0,"rat=cr",
                       "rfm=var")) == ERROR1)
      {
         perror("FILE3: creat() failed\n");
         exit(2);
      }

   if ((fp = fdopen(fildes,"w")) == NULL)
      {
         perror("FILE3: fdopen() failed\n");
         exit(2);
      }
   while(fgets(buffer,BUFFSIZE,stdin) != NULL)
      if (fwrite(buffer,strlen(buffer),1,fp) == ERROR)
         {
            perror("FILE3: fwrite() failed\n");
            exit(2);
         }

   if (fclose(fp) == EOF)
      {
         perror("FILE3: fclose() failed\n");
         exit(2);
      }
}


Previous Page | Next Page | Table of Contents | Index