Hi,
I am working on a DOS application (16-bit) and stumbled @ a point where I need to pass a variable to mkdir and chdir functions and they don't seem to recognize the string and always fail the code. After executing the below code, I always get 'Cannot copy file ! Press key to exit.' Can someone please tell me if I am doing something wrong here?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<dir.h>
void main(int arg,char *arr[])
{
FILE *fs=NULL,*fd=NULL;
struct find_t fileinfo;
char c,bb[_MAX_PATH],choice,param[10],drive,path[100],*dir=NULL;
char *DIRPATH="\\LongDi~1\\\\SecDir01",*PATHNAME="\\mynewfile\.txt";
int i,d,ch,p,found=0;
clrscr();
if(arg!=2)
{
printf("\n Usage: flcpy <file name>");
printf("\n E.g. flcpy mynewfile.txt");
exit(1);
}
//Check if source file exists
if((fs=fopen(arr[1],"rb"))==NULL)
{
printf("\n Cannot open source file ! Press key to exit.");
getch();
exit(1);
}
// Code to determine which partition has TEST_DR volume label
printf("\n Looking for TEST_DR partition:");
for(c='A';c<='Z';c++)
{
sprintf(param,"%c:\\*.*",c);
param[strlen(param)+1]='\0';
if((!_dos_findfirst(param,_A_VOLID, &fileinfo)) && (!strcmp(fileinfo.name,"TEST_DR")))
{
found=1;
drive=c;
}
}
if (found==1)
{
printf("\n TEST_DR partition %c: is found....Copying the file",drive);
//path variable will be used as destination file into which text file will be copied
sprintf(path,"%c:\\%s\\%s",drive,DIRPATH,PATHNAME);
//path[strlen(path)]='\0';
printf("\nPath=%s",path);
//dir variable will be used as the dest dir where the text file will be copied
d=sprintf(dir,"%c:\\%s",drive,DIRPATH);
//printf("\nDirLen=%d,%d",strlen(dir),d);
//dir[d]='\0';
printf("\nDir=%s\n",*dir);
}
else
{
printf("\n ERROR: Cannot copy the file...partition not found");
exit(1);
}
//getcwd(bb,_MAX_PATH);
//printf("\n%s",bb);
p=chdir(dir);
printf("\n%d",p);
if (p!=0)
mkdir(dir);
if((fd=fopen(path,"rb"))!=NULL)
{
do
{
printf("\n Destination file already exists!! Choose to overwrite? (Y/N):");
choice=getche();
}while ((choice!='Y') && (choice!='N') && (choice!='y') && (choice!='n'));
fclose(fd);
if ((choice=='N') || (choice=='n'))
exit(1);
}
if((fd=fopen(path,"wb"))==NULL)
{
printf("\n Cannot copy file ! Press key to exit.");
getch();
exit(1);
}
while(!feof(fs))
{
ch = fgetc(fs);
fputc(ch,fd);
}
printf("\n File copied succesfully!");
fclose(fs);
fclose(fd);
}