But my demand is the program which specifies and copies a file.
I want to specify not the whole of FDD but a file.
OK, I'm not sure if this will help you or not.... I have written this copy program for you to have a look at.
It allows you to run it in any of the following ways:
>mycopy a:\myfile.txt
- This will copy to the current working directory, with filename of myfile.txt
>mycopy a:\myfile.txt mynewfile.txt
- This will copy to the current working directory, with filename of mynewfile.txt
>mycopy a:\myfile.txt c:\mydir\mynewfile.txt
- This will copy to the directory c:\mydir, with filename of mynewfile.txt
It assumes you are using DOS style filenames (ie backslashes).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL_NAME 128
char *MakeFileName(char *, char *);
int main(int argc, char *argv[])
{
FILE *ifp, *ofp;
char fromfile[MAXL_NAME], tofile[MAXL_NAME];
int c;
switch (argc)
{
case 2:
strncpy(fromfile, argv[1], sizeof (fromfile));
if (MakeFileName(fromfile, tofile) == NULL)
{
printf ("Cannot set second filename\n");
return (EXIT_FAILURE);
}
break;
case 3:
strncpy(fromfile, argv[1], sizeof (fromfile));
strncpy(tofile, argv[2], sizeof (tofile));
break;
default:
printf ("No filename specified\n");
return (EXIT_FAILURE);
break;
}
fromfile[MAXL_NAME-1] = '\0';
tofile[MAXL_NAME-1] = '\0';
if ((ifp = fopen(fromfile, "rb")) == NULL)
{
perror (fromfile);
return (EXIT_FAILURE);
}
if ((ofp = fopen(tofile, "wb")) == NULL)
{
perror (tofile);
return (EXIT_FAILURE);
}
printf ("Copy from %s to %s\n", fromfile, tofile);
while ((c = fgetc(ifp)) != EOF && fputc(c, ofp) != EOF);
if (ferror(ifp))
perror(fromfile);
if (ferror(ofp))
perror(tofile);
printf ("Complete\n");
fclose (ifp);
fclose (ofp);
return EXIT_SUCCESS;
}
char *MakeFileName(char *in, char *out)
{
/*
* This function takes a fully qualified filename (in) and
* copies only the non-qualified name to out.
* Allow for two types of filename:
* c:\mydir\myfile.txt
* c:myfile.txt
* In either case the out variable will contain myfile.txt
* On success a pointer to out is return,
* on failure NULL is returned.
*/
char *p;
if (in == NULL || out == NULL)
return (NULL);
if ((p = strrchr(in, '\\\')) == NULL)
if ((p = strrchr(in, ':')) == NULL)
return (NULL);
/* Ensure that the slash or colon wasn't the
* last character in the string
*/
if (++p)
strcpy (out, p);
else return (NULL);
return (out);
}
If this isn't what you want, can you rephrase your question please.
Have fun