A Simple-Minded File-Condensing Program

I l @ ve RuBoard

A Simple-Minded File- Condensing Program

This next program copies selected data from one file to another. It opens two files simultaneously , using the "r" mode for one and the "w" mode for the other. The program (see Listing 12.2) condenses the contents of the first file by the brutal expedient of retaining only every third character. Finally, it places the condensed text into the second file. The name for the second file is the old name with .red (for reduced) appended. Command-line arguments, opening more than one file simultaneously, and filename appending are generally quite useful techniques. This particular form of condensing is of more limited appeal , but it can have its uses, as you will see.

Listing 12.2 The reducto.c program.
 /* reducto.c -- reduces your files by two-thirds ! */ #include <stdio.h> #include <stdlib.h> #include <string.h>      /* for strcpy(), strcat() */ int main(int argc, char *argv[]) {     FILE  *in, *out;   /* declare two FILE pointers       */     int ch;     char name[40];     /* storage for output filename     */     int count = 0;     if (argc < 2)      /* check for an input file         */     {          fprintf(stderr, "Usage: %s filename\n", argv[0]);          exit(1);     }     if ((in = fopen(argv[1], "r")) == NULL)     {         fprintf(stderr, "I couldn't open the file \"%s\"\n",                 argv[1]);         exit(2);     }     strcpy(name,argv[1]);   /* copy filename into array   */     strcat(name,".red");    /* append .red to name        */     if ((out = fopen(name, "w")) == NULL)     {                       /* open file for writing      */         fprintf(stderr,"Can't create output file.\n");         exit(3);     }     while ((ch = getc(in)) != EOF)         if (count++ % 3 == 0)             putc(ch, out);  /* print every 3rd char       */     if (fclose(in) != 0  fclose(out) != 0)         fprintf(stderr,"Error in closing files\n");     return 0;     } 

The executable file is called reducto . We applied it to a file called eddy , which contained this single line:

 So even Eddy came oven ready. 

The command was as follows :

 reducto eddy 

The output was written to a file called eddy.red . The program doesn't produce any onscreen output, but displaying the eddy.red file revealed the following:

 Send money 

This example illustrates several programming techniques. Let's examine some of them now.

The fprintf() function is like printf() except that it requires a file pointer as its first argument. We've used the stderr pointer to send error messages to the standard error; this is a standard C practice.

To construct the new name for the output file, the program uses strcpy() to copy the name eddy into the array name . Then it uses the strcat() function to combine that name with .red , producing eddy.red . We also checked to see whether the program succeeded in opening a file by that name. This is particularly important in the DOS environment because a filename like, say, strange .c.red is invalid; you can't add extensions to extensions under DOS. (The proper MS-DOS approach is to replace any existing extension with .red , so that the reduced version of strange.c would be strange.red . You could use the strchr () function, for example, to locate the period, if any, in a name and copy only the part of the string before the period.)

This program had two files open simultaneously, so we declared two FILE pointers. Note that each file is opened and closed independently of the other. There are limits to how many files you can have open at one time. The limit depends on your system and implementation; the range is often 10 to 20. You can use the same file pointer for different files, providing those files are not open at the same time.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net