Thread: Input/Output Files

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Input/Output Files

    I am in need of some guidance for a problem in my CS class. The problem is stated as follows:

    Rewrite the file backup program in figure 12.1 so it uses a function with file pointer parameters to do the actual file copy.

    Here is figure 12.1 >
    Code:
    Figure 12.1  Program to Make a Backup Copy of a Text File
    /*
     *  Makes a backup file.  Repeatedly prompts for the name of a file to 
     *  back up until a name is provided that corresponds to an available 
     *  file.  Then it prompts for the name of the backup file and creates 
     *  the file copy.
     */
    
    #include <stdio.h>
    #define  STRSIZ 80
     
    int
    main(void)
    {
          char  in_name[STRSIZ], 	/* strings giving names              	*/
                out_name[STRSIZ];	/*    of input and backup files     	 */
          FILE *inp,            	/* file pointers for input and      	 */
               *outp;         	/*    backup files                 	  */
          char ch;              	/* one character of input file	*/
    
          /*  Get the name of the file to back up and open the file for 
    input      	*/
          printf("Enter name of file you want to back up> ");
          for  (scanf("%s", in_name);
                (inp = fopen(in_name, "r")) == NULL;
                 scanf("%s", in_name)) {
              printf("Cannot open %s for input\n", in_name);
              printf("Re-enter file name> ");
          }
     
          /*  Get name to use for backup file and open file for output 	*/
          printf("Enter name for backup copy> ");
          for  (scanf("%s", out_name);
                (outp = fopen(out_name, "w")) == NULL;
                 scanf("%s", out_name)) {
              printf("Cannot open %s for output\n", out_name);
              printf("Re-enter file name> ");
          }
     
          /*  Make backup copy one character at a time	*/
          for  (ch = getc(inp);  ch != EOF;  ch = getc(inp))
              putc(ch, outp);
     
          /*  Close files and notify user of backup completion	*/
          fclose(inp);
          fclose(outp);
          printf("Copied %s to %s.\n", in_name, out_name);
     
          return(0);
    }
    I dont really understand what the question means, from what i see it looks like it already uses file pointers (inp and outp) to do the copy. If there is anyway you could help guide me
    it would be greatly appreciated.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Write a function to do the file copying, and make it accept the FILE *'s as parameters instead of doing something silly like using global variables.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    2
    is there any way you could give me an example of what it may look like.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you can't figure it out, you must not be paying attention in class. This is ridiculously easy. Don't waste our time. Read the lecture slides or whatever material the professor has for you.

    Just so you can't say I didn't try to help, I'll spell it out again: Make a simple function. Make it accept the FILE *'s. Make it do what the code you already have does. This should take you no longer than 5 minutes on a bad day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  2. Input/Output to external files, I NEED HELP!!
    By MrMax91423 in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2006, 08:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM