-
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.
-
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.
-
is there any way you could give me an example of what it may look like.
-
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.