Chapter 2 Introduction to the C Language by hmv21438

VIEWS: 14 PAGES: 21

									             ISTITUTO TECNICO INDUSTRIALE STATALE "E. MATTEI"
           ESERCITAZIONE DI LABORATORIO (INFORMATICA A.S. 02/03)

Classe: Quarta "Ainf"
Prof. Tessore Luca

             ESEMPI DI FUNZIONI RELATIVE A FILE DI TESTO
                         (Esempi n°21 – n°42)

 21.
       /* ==================== appendFile ====================
          This function appends one file to the other.
            Pre   Files exist
            Post File 2 appended to file 1.
                  Returns 0 if successful
                          1 if file 1 cannot be opened
                          2 if file 2 cannot be opened
       */
       int appendFile (const char * file1, const char * file2)
       {
       /* Local Definitions */
          char c;
          FILE *fp1;
          FILE *fp2;

       /* Statements */
          if (!(fp1 = fopen (file1, "a")))
             {
               printf ("\nError opening %s for appending.\n",
                       file1);
               return (1);
             } /* if */

            if (!(fp2 = fopen (file2, "r")))
               {
                 printf ("\nError opening %s for reading.\n",
                          file2);
                 return (2);
               } /* if */

            while ((c = fgetc (fp2)) != EOF)
                fputc (c, fp1);

            fclose (fp1);
            fclose (fp2);
            return 0;
       }    /* appendFile */
 22.
       /* =================== appendItself ===================
          This function appends the file to itself.
             Pre File exists
             Post File appended to itself
       */
       int appendItself (const char * filename)
       {

                                       Page 7.1
      /* Local Definitions */
         char c;
         char *tempfile = "TEMP.DAT";
         FILE *fp;
         FILE *fptemp;

      /* Statements */
         if (!(fp = fopen (filename, "r")))
            {
              printf ("\nError opening %s for reading.\n",
                       filename);
              return (1);
            } /* if open */

          if (!(fptemp = fopen (tempfile, "w")))
             {
               printf ("\nError opening %s for writing.\n",
                        tempfile);
               return (2);
             } /* if open */

          while ((c = fgetc (fp)) != EOF)
              fputc (c, fptemp);

          fclose (fp);
          fclose (fptemp);

          if (!(fp = fopen (filename, "a")))
             {
               printf ("\nError opening %s for appending.\n",
                       filename);
               return (1);
             } /* if open error */

          if (!(fptemp = fopen (tempfile, "r")))
             {
               printf ("\nError opening %s for reading.\n",
                        tempfile);
               return (2);
             } /* if open error */

          while ((c = fgetc (fptemp)) != EOF)
              fputc (c, fp);

          fclose (fp);
          fclose (fptemp);
          return 0;
      }   /* appendItself */
23.
      /* ==================== formatLine ====================
         This function reformats a file to 60 characters per
         line.
            Pre   File exists
            Post File reformatted 60 characters per line
      */

      int formatLine (const char * filename)
      {

                                     Page 7.2
/* Local Definitions */
   char c;
   char *tempfile = "TEMP.DAT";
   int   count    = 0;
   FILE *fp;
   FILE *fptemp;

/* Statements */
   if (!(fp = fopen (filename, "r")))
      {
        printf ("\nError opening %s for reading.\n",
                filename);
        return (1);
      } /* if open error */

   if (!(fptemp = fopen (tempfile, "w")))
      {
        printf ("\nError opening %s for writing.\n",
                tempfile);
        return (2);
      } /* if open error */

   while ((c = fgetc (fp)) != EOF)
         {
           if (count == 60)
              {
                fputc ('\n', fptemp);
                count = 0;
              } /* if count */
           if (c != '\n')
              {
                fputc (c, fptemp);
                count++;
              } /* if not \n */
         } /* while */
   fputc ('\n', fptemp);

   fclose (fp);
   fclose (fptemp);

   if (!(fp = fopen (filename, "w")))
      {
        printf ("\nError opening %s for writing.\n",
                 filename);
        return (1);
      } /* if open error */

   if (!(fptemp = fopen (tempfile, "r")))
      {
        printf ("\nError opening %s for reading.\n",
                 tempfile);
        return (2);
      } /* if open error */

   while ((c = fgetc (fptemp)) != EOF)
       fputc (c, fp);

   fclose (fp);

                               Page 7.3
          fclose (fptemp);
          return 0;
      }   /* formatLine */
24.
      /* ==================== aveCharLine ===================
         This function calculates the average numbers of
         characters per line in a file.
             Pre    File exists
             Post average characters per line printed
      */
      int aveCharLine (const char * filename)
      {
      /* Local Definitions */
         char    c;
         int     line_cnt = 0;
         int     char_cnt = 0;
         float ave;
         FILE *fp;

      /* Statements    */
         if (!(fp =    fopen (filename, "r")))
            {
              printf   ("\nError opening %s.\n", filename);
              return   (1);
            } /* if    open error */

          while ((c = fgetc(fp)) != EOF)
             {
               if (c == '\n')
                    line_cnt++;
               else
                    char_cnt++;
             } /* while */

          ave = (float) char_cnt / line_cnt;

          fclose (fp);

          printf ("\n# of characters: %d, # of lines: %d\n",
                   char_cnt, line_cnt);
          printf ("\nAverage characters/line: %4.1f\n", ave);

          return 0;
      }   /* aveCharLine */
25.
      /* ==================== delLastLine ===================
         This function deletes the last line of any file.
             Pre    File exists
             Post Last line of file deleted.
      */
      int delLastLine (const char * filename)
      {
      /* Local Definitions */
         char c;
         char *tempfile = "TEMP.DAT";
         int     line_count = 1;
         int     total_lines = 1;
         FILE fp;

                                       Page 7.4
          FILE   fptemp;

      /* Statements    */
         if (!(fp =    fopen (filename, "r")))
            {
              printf   ("\nError opening %s.\n", filename);
              return   (1);
            } /* if    open error */

          if (!(fptemp = fopen (tempfile, "w")))
             {
               printf ("\nError opening %s.\n", tempfile);
               return (2);
             } /* if open error */

          /* count the number of lines and build temp file */
          while ((c = fgetc (fp)) != EOF)
             {
               if (c == '\n')
                   total_lines++;
               fputc(c, fptemp);
             } /* while */

          fclose (fp);
          fclose (fptemp);

          if (!(fp = fopen (filename, "w")))
             {
               printf ("\nError opening %s for writing.\n",
                        filename);
               return (1);
             } /* if open error */

          if (!(fptemp = fopen (tempfile, "r")))
             {
               printf ("\nError opening %s for reading.\n",
                        tempfile);
               return (2);
             } /* if open error */

          /* Write all but last line to the original file. */
          while ((c = fgetc (fptemp)) != EOF
               && line_count < total_lines - 1)
             {
               fputc (c, fp);
               if (c == ’\n’)
                  line_count++;
             } /* while */

          fclose (fp);
          fclose (fptemp);

          return 0;
      }   /* delLastLine */
26.
      /* ================ delBlankLine ================
         This function deletes the blank lines in a file.
            Pre   File exists

                                       Page 7.5
            Post the blank lines of file deleted
      */
      int delBlankLine (const char * filename)
      {
      /* Local Definitions */
         char cur;
         char pre = '\n';
         char *tempfile = "TEMP.DAT";
         FILE *fp;
         FILE *fptemp;

      /* Statements    */
         if (!(fp =    fopen (filename, "r")))
            {
              printf   ("\nError opening %s.\n", filename);
              return   (1);
            } /* if    open error */

          if (!(fptemp = fopen (tempfile, "w")))
             {
               printf ("\nError opening %s.\n", tempfile);
               return (2);
             } /* if open error */

          while ((cur = fgetc(fp)) != EOF)
             {
               if (pre != '\n' || cur != '\n')
                  fputc (cur, fptemp);
               pre = cur;
             } /* while */

          fclose (fp);
          fclose (fptemp);

          if (!(fp = fopen (filename, "w")))
             {
               printf ("\nError opening %s for writing.\n",
                        filename);
               return (1);
             } /* if open error */

          if (!(fptemp = fopen (tempfile, "r")))
             {
               printf ("\nError opening %s for reading.\n",
                        tempfile);
               return (2);
             } /* if open error */

          while ((cur = fgetc (fptemp)) != EOF)
              fputc (cur, fp);

          fclose (fp);
          fclose (fptemp);
          return 0;
      }   /* delBlankLine */
27.
      /* This program prints itself.
            Written by:

                                       Page 7.6
            Date:
      */
      #include <stdio.h>

      int main (void)
      {
      /* Local Definitions */
         char c;
         FILE *fp;

      /* Statements */
         printf ("\n *** start of prb0727 ***\n\n");
         if (!(fp = fopen ("prb0727.c", "r")))
            {
              printf ("\nError opening prb0727.c.\n");
              return (1);
            } /* if open error */

          while ((c = fgetc(fp)) != EOF)
                 printf ("%c", c);

          fclose (fp);
          printf ("\n ***   end   of prb0727 ***\n\n");
          return 0;
      }   /* main */
28.
      /* This program copies one text file to another and
         inserts blank lines between paragraphs in new file.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "FILE2.DAT"

      int main (void)
      {
      /* Local Definitions */
         char c;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0728 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);
             } /* if open error */


                                       Page 7.7
          while ((c = fgetc(fp1)) != EOF)
             {
               if (c == '\n')
                  fputc ('\n', fp2);
               fputc (c, fp2);
             } /* while */

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0728 ***\n\n");
          return 0;
      }   /* main */
29.
      /* This program copies only the line started with a
         specified character.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "FILE2.DAT"

      int main (void)
      {
      /* Local Definitions */
         char target;
         char cur;
         char pre = '\n';
         int   writeLn = 0;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0729 ***\n\n");

          printf ("Please enter the target character:     ");
          scanf ("%c", &target);

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);
             } /* if open error */

          while ((cur = fgetc(fp1)) != EOF)
             {
               if (!writeLn && pre == '\n' && cur == target)
                  writeLn = 1;
               if (writeLn)
                  fputc (cur, fp2);
               if (writeLn && cur == '\n')

                                       Page 7.8
                 writeLn = 0;
              pre = cur;
             } /* while */

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0729 ***\n\n");
          return 0;
      }   /* main */

32.
      /* This program inserts a blank line after the seventh
         line in a file.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "TEMP.DAT"

      int main (void)
      {
      /* Local Definitions */
         char c;
         int   line_cnt = 0;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0732 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);
             } /* if open error */

          while ((c = fgetc(fp1)) != EOF)
             {
               if (c == '\n')
                  line_cnt++;
               if (c == '\n' && line_cnt == 7)
                  fputc ('\n', fp2);
               fputc (c, fp2);
             } /* while */

          fclose (fp1);
          fclose (fp2);

          if (!(fp1 = fopen (FILE_1, "w")))
             {

                                       Page 7.9
              printf ("\nError opening %s for writing.\n",
                       FILE_1);
              return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "r")))
             {
               printf ("\nError opening %s for reading.\n",
                        FILE_2);
               return (2);
             } /* if open error */

          while ((c = fgetc (fp2)) != EOF)
              fputc (c, fp1);

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0732 ***\n\n");
          return 0;
      }   /* main */
33.
      /* This program deletes the sixth line in a file.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "TEMP.DAT"

      int main (void)
      {
      /* Local Definitions */
         char ch;
         int   line_cnt = 0;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0733 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);
             } /* if open error */

          while ((ch = fgetc(fp1)) != EOF)
             {
               if (ch == '\n')
                   line_cnt++;
               if (line_cnt != 5)

                                      Page 7.10
                  fputc (ch, fp2);
             } /* while */

          fclose (fp1);
          fclose (fp2);

          if (!(fp1 = fopen (FILE_1, "w")))
             {
               printf ("\nError opening %s for writing.\n",
                        FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "r")))
             {
               printf ("\nError opening %s for reading.\n",
                        FILE_2);
               return (2);
             } /* if open error */

          while ((ch = fgetc (fp2)) != EOF)
              fputc (ch, fp1);

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0733 ***\n\n");
          return 0;
      }   /* main */
34.
      /* Inserts a blank line after each line in the file.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "TEMP.DAT"

      int main (void)
      {
      /* Local Definitions */
         char ch;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0734 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);

                                      Page 7.11
             } /* if open error */

          while ((ch = fgetc(fp1)) != EOF)
             {
               if (ch == '\n')
                   fputc (’\n’, fp2);
               fputc (ch, fp2);
             } /* while */

          fclose (fp1);
          fclose (fp2);

          if (!(fp1 = fopen (FILE_1, "w")))
             {
               printf ("\nError opening %s for writing.\n",
                        FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "r")))
             {
               printf ("\nError opening %s for reading.\n",
                        FILE_2);
               return (2);
             } /* if open error */

          while ((ch = fgetc (fp2)) != EOF)
              fputc (ch, fp1);

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0734 ***\n\n");

          return 0;
      }   /* main */
35.
      /* This program duplicates the fourth line in a file.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "TEMP1.DAT"
      #define FILE_3 "TEMP2.DAT"

      int main (void)
      {
      /* Local Definitions */
         char c;
         int   line_cnt = 0;
         FILE *fp1;
         FILE *fp2;
         FILE *fp3;

      /* Statements */
         printf ("\n *** start of prb0735 ***\n\n");


                                      Page 7.12
if (!(fp1 = fopen (FILE_1, "r")))
   {
     printf ("\nError opening %s.\n", FILE_1);
     return (1);
   } /* if open error */

if (!(fp2 = fopen (FILE_2, "w")))
   {
     printf ("\nError opening %s.\n", FILE_2);
     return (2);
   } /* if open error */

if (!(fp3 = fopen (FILE_3, "w")))
   {
     printf ("\nError opening %s for writing.\n",
                FILE_3);
     return (3);
   } /* if open error */
while ((c = fgetc(fp1)) != EOF)
   {
     if (c == '\n')
          line_cnt++;
     if (line_cnt == 3)
          fputc (c, fp3);
     if (line_cnt == 4 && c == '\n')
        {
          fclose (fp3);
          if (!(fp3 = fopen (FILE_3, "r")))
             {
               printf ("\nError 4 opening %s.\n",
                         FILE_3);
               return (4);
             } /* if */
          while ((c = fgetc(fp3)) != EOF)
               fputc (c, fp2);
          c = '\n';
          fclose(fp3);
        } /* if line_cnt */
     fputc (c, fp2);
   } /* while */

fclose (fp1);
fclose (fp2);

if (!(fp1 = fopen (FILE_1, "w")))
   {
     printf ("\nError opening %s for writing.\n",
              FILE_1);
     return (1);
   } /* if open error */

if (!(fp2 = fopen (FILE_2, "r")))
   {
     printf ("\nError opening %s for reading.\n",
              FILE_2);
     return (2);
   } /* if open error */


                           Page 7.13
          while ((c = fgetc (fp2)) != EOF)
              fputc (c, fp1);

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0735 ***\n\n");
          return 0;
      }   /* main */
36.
      /* This program deleting the first two characters from
         each line of a file.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "FILE2.DAT"

      int main (void)
      {
      /* Local Definitions */
         char c;
         int   char_cnt = 0;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0736 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);
             } /* if open error */

          while ((c = fgetc(fp1)) != EOF)
             {
               if (c != '\n')
                  char_cnt++;
               else
                  char_cnt = 0;

              if (char_cnt != 1 && char_cnt != 2)
                 fputc (c, fp2);
             } /* while */

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0736 ***\n\n");
          return 0;
      }   /* main */

                                      Page 7.14
37.
      /* This program copies a file, inserting two space
         characters at the beginning of each line.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "FILE2.DAT"

      int main (void)
      {
      /* Local Definitions */
         char cur;
         char pre = '\n';
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0737 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);
             } /* if open error */

          while ((cur = fgetc(fp1)) != EOF)
             {
               if (pre == '\n')
                  {
                    fputc (' ', fp2);
                    fputc (' ', fp2);
                  } /* if */
               fputc (cur, fp2);
               pre = cur;
             } /* while */

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0737 ***\n\n");
          return 0;
      }   /* main */
38.
      /* This program creates a file consisting of the 21st
         character of each line in a file.
            Written by:
            Date:
      */
      #include <stdio.h>
      #include <ctype.h>

                                      Page 7.15
      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "FILE2.DAT"

      int main (void)
      {

      /* Local Definitions */
         char cur;
         char pre;
         int   char_no   = 0;
         int   all_white = 1;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0738 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          if (!(fp2 = fopen (FILE_2, "w")))
             {
               printf ("\nError opening %s.\n", FILE_2);
               return (2);
             } /* if open error */

          while ((cur = fgetc(fp1)) != EOF)
             {
               char_no++;
               if (!isspace(cur))
                  all_white = 0;
               if (cur == ’\n’)
                  {
                    if (char_no < 21 && !all_white)
                       fputc (pre, fp2);
                    char_no   = 0;
                    all_white = 1;
                  } /* if cur */
               if (char_no == 21 && !all_white)
                  fputc (cur, fp2);
               pre = cur;
             } /* while */

          fputc (’\n’, fp2);

          fclose (fp1);
          fclose (fp2);
          printf ("\n ***   end   of prb0738 ***\n\n");
          return 0;
      }   /* main */
39.
      /* This program writes the odd numbers between
         and 500 to a text file.
            Written by:

                                      Page 7.16
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"

      int main (void)
      {
      /* Local Definitions */
         int   number;
         FILE *fp1;

      /* Statements */
         printf ("\n *** start of prb0739 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "w")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);
             } /* if open error */

          for (number = 301; number < 500; number += 2)
             fprintf (fp1, "%d\n", number);

          fclose (fp1);
          printf ("\n ***   end   of prb0739 ***\n\n");
          return 0;
      }   /* main */
40.
      /* This program is a test driver for multiples.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define   OUT_FILE "FILE1.DAT"
      #define   MULT      3
      #define   LOWER     10
      #define   UPPER     20

      /* Prototype Declarations */
      int multiples (const char *filename,
                            int    num,
                            int    lowLimit,
                            int    highLimit);

      int main (void)
      {
      /* Statements */
         printf ("\n *** start of prb0740 ***\n\n");

          multiples (OUT_FILE, MULT, LOWER, UPPER);

          printf ("\n ***   end   of prb0740 ***\n\n");
          return 0;
      }   /* main */

      /* ==================== multiples ===================

                                       Page 7.17
          This function writes the multiples of num between
          given limits to a text file.
             Pre filename is the file to create
                  num is the number to use for multiples
                  lowLimit is the lower limit
                  highLimit is the higher limit
             Post file has been created or an error returned
      */
      int multiples (const char *filename,
                           int   num,
                           int   lowLimit,
                           int   highLimit)
      {
      /* Local Definitions */
         int i;
         FILE *fp;

      /* Statements */
         if (!(fp = fopen (filename, "w")))
            {
              printf ("\nError opening %s for writing.\n",
                       filename);
              return (1);
            } /* if open error */

          for (i = lowLimit; i <= highLimit; i++)
             if (i % num == 0)
                fprintf (fp, "%d\n", i);

          fclose (fp);
          return 0;
      }   /* multiples */
41.
      /* This program reads a file of scores and creates a new
         file of all of the scores over 90.
            Written by:
            Date:
      */
      #include <stdio.h>

      #define FILE_1 "FILE1.DAT"
      #define FILE_2 "FILE2.DAT"

      int main (void)
      {
      /* Local Definitions */
         int   score;
         int   count = 0;
         FILE *fp1;
         FILE *fp2;

      /* Statements */
         printf ("\n *** start of prb0741 ***\n\n");

          if (!(fp1 = fopen (FILE_1, "r")))
             {
               printf ("\nError opening %s.\n", FILE_1);
               return (1);

                                     Page 7.18
               }    /* if open error */

           if (!(fp2 = fopen (FILE_2, "w")))
              {
                printf ("\nError opening %s.\n", FILE_2);
                return (2);
              } /* if open error */

           while ((fscanf(fp1, "%d", &score)) != EOF)
              {
                if (score >= 90)
                   {
                     fprintf (fp2, "%d\n", score);
                     count++;
                   } /* if */
              } /* while */

           printf ("\nThere were %d scores over 90.\", count);

           fclose (fp1);
           fclose (fp2);
           printf ("\n ***        end    of prb0741 ***\n\n");
           return 0;
      }    /* main */
42.
      /*   This program reads a file of scores and creates new
           files of different ranges of scores.
                Written by:
                Date:
      */
      #include <stdio.h>

      #define      LOW_RANGE       score > 0
      #define      MED_RANGE       score > 30
      #define      HIGH_RANGE      score > 60
      #define      BEST_RANGE      score > 90
      #define      SOURCE_FILE    "FILE1.DAT"
      #define      LOWS_FILE      "LOWS.DAT"
      #define      MEDS_FILE      "MEDS.DAT"
      #define      HIGHS_FILE     "HIGHS.DAT"
      #define      BEST_FILE      "BEST.DAT"

      int main (void)
      {
      /* Local Definitions */
         int   score;
         int   low_count = 0;
         int   med_count = 0;
         int   high_count = 0;
         int   best_count = 0;
         FILE *fpsource;
         FILE *fplows;
         FILE *fpmeds;
         FILE *fphighs;
         FILE *fpbest;

      /* Statements */
         printf ("\n *** start of prb0742 ***\n\n");

                                                Page 7.19
if (!(fpsource = fopen (SOURCE_FILE, "r")))
   {
     printf ("\nError opening %s.\n", SOURCE_FILE);
     return (1);
   } /* if open error */

if (!(fplows = fopen (LOWS_FILE, "w")))
   {
     printf ("\nError opening %s.\n", LOWS_FILE);
     return (2);
   } /* if open error */

if (!(fpmeds = fopen (MEDS_FILE, "w")))
   {
     printf ("\nError opening %s.\n", MEDS_FILE);
     return (2);
   } /* if open error */

if (!(fphighs = fopen (HIGHS_FILE, "w")))
   {
     printf ("\nError opening %s.\n", HIGHS_FILE);
     return (1);
   } /* if open error */

if (!(fpbest = fopen (BEST_FILE, "w")))
   {
     printf ("\nError opening %s.\n", BEST_FILE);
     return (2);
   } /* if open error */

while ((fscanf(fpsource, "%d", &score)) != EOF)
      {
        if (BEST_RANGE)
           {
             fprintf (fpbest, "%d\n", score);
             best_count++;
           } /* if best */
        else if (HIGH_RANGE)
           {
             fprintf (fphighs, "%d\n", score);
             high_count++;
           } /* if high */
        else if (MED_RANGE)
           {
             fprintf (fpmeds, "%d\n", score);
             med_count++;
           } /* if med */
        else if (LOW_RANGE)
           {
             fprintf (fplows, "%d\n", score);
             low_count++;
           } /* if low */
      } /* while */

printf ("\nThere were %d scores in best range.\n",
         best_count);
printf ("There were %d scores in high range.\n",

                           Page 7.20
             high_count);
    printf ("There were %d scores in medium range.\n",
             med_count);
    printf ("There were %d scores in low range.\n\n",
             low_count);

    fclose (fpsource);
    fclose (fpbest);
    fclose (fphighs);
    fclose (fpmeds);
    fclose (fplows);
    printf ("\n *** end   of prb0742 ***\n\n");
    return 0;
}   /* main */




                              Page 7.21

								
To top