Thread: Trouble with mkdir chdir

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    7

    Trouble with mkdir chdir

    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);
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Because there's no storage associated with the char *dir variable.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    7
    Hi itCbitC, thanks for the reply. I actually changed my code to the following but still see the problem.

    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[100];
    	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);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    At first glance, you've got about 2.5 times as many backslashes as you would want (assuming you want valid file paths). Right: "mynewfile.txt" Wrong: "\\mynewfile\.txt" (just as a for-instance). In fact, this:
    Code:
    sprintf(path,"%c:\\%s\\%s",drive,DIRPATH,PATHNAME);
    gives you all the backslashes needed in your path (except the one that is needed in your DIRPATH, and that one backslash should be indicated by \\.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    7
    Hi tabstop,
    Thanks for the reply. If I take "mynewfile.txt", then the output of
    Code:
    printf("\nPath=%s",path);
    after the above sprintf statement is given as 'E:\\LongDi~1\\SecDir01\mynewfile.txt'. Which will not work in chdir. With mmy current code, I get
    Path=E:\\LongDi~1\\SecDir01\\mynewfile.txt
    DirLen=22,22
    Dir=E:\\LongDi~1\\SecDir01


    But for some reason the output of chdir(dir) gives -1.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because E:\\LongDi~1\\SecDir01\\mynewfile.txt is so amazingly not a valid path that no one is at all surprised that it doesn't work. If you want a real path, you only want one backslash. I mean, pop open a command prompt and look at what a path really looks like -- there aren't any double backslashes there.

    With the sprintf line I quoted, you want DIRPATH to be
    "LongDi~1\\SecDir01"
    and PATHNAME to be
    "mynewfile.txt"
    and everyone will be happy.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by tabstop View Post
    That's because E:\\LongDi~1\\SecDir01\\mynewfile.txt is so amazingly not a valid path that no one is at all surprised that it doesn't work. If you want a real path, you only want one backslash. I mean, pop open a command prompt and look at what a path really looks like -- there aren't any double backslashes there.

    With the sprintf line I quoted, you want DIRPATH to be
    "LongDi~1\\SecDir01"
    and PATHNAME to be
    "mynewfile.txt"
    and everyone will be happy.
    Heheh, programmer cynicism at it's finest.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I am working on a DOS application (16-bit)
    Why?

    I mean with all those munged long pathnames (6 letters, ~digit), you're obviously hosting this on a 32 bit OS.

    So why aren't you using a compiler which is actually matched to your OS, and not some 20 year old relic?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    7
    The reason being the tool has to work in 16-bit environment (something like FreeDOS) that we can boot to quickly and also it is the only place (real mode) where I can make BIOS calls.

    Quote Originally Posted by Salem View Post
    > I am working on a DOS application (16-bit)
    Why?

    I mean with all those munged long pathnames (6 letters, ~digit), you're obviously hosting this on a 32 bit OS.

    So why aren't you using a compiler which is actually matched to your OS, and not some 20 year old relic?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    7
    I guess you didn't get me.

    Code:
    if (chdir("E:\LongDi~1\SecDir01")!=0)
    mkdir("E:\LongDi~1\SecDir01");
    is not creating any 'SecDir01' directory whereas

    Code:
    if (chdir("E:\\LongDi~1\\SecDir01")!=0)
    mkdir("E:\\LongDi~1\\SecDir01");
    creates the required directory.

    However even if the dir variable reports as "E:\\LongDi~1\\SecDir01", when I pass it as chdir(dir) or chdir("dir") doesn't really create the required directory.

    FYI I am working on BorlandC compiler to support 16-bit target


    Quote Originally Posted by tabstop View Post
    That's because E:\\LongDi~1\\SecDir01\\mynewfile.txt is so amazingly not a valid path that no one is at all surprised that it doesn't work. If you want a real path, you only want one backslash. I mean, pop open a command prompt and look at what a path really looks like -- there aren't any double backslashes there.

    With the sprintf line I quoted, you want DIRPATH to be
    "LongDi~1\\SecDir01"
    and PATHNAME to be
    "mynewfile.txt"
    and everyone will be happy.
    Last edited by scripterJack; 05-22-2010 at 03:47 PM. Reason: Incomplete post

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by scripterJack View Post
    I guess you didn't get me.

    Code:
    if (chdir("E:\LongDi~1\SecDir01")!=0)
    mkdir("E:\LongDi~1\SecDir01");
    is not creating any 'SecDir01' directory whereas

    Code:
    if (chdir("E:\\LongDi~1\\SecDir01")!=0)
    mkdir("E:\\LongDi~1\\SecDir01");
    creates the required directory.
    Exactly. So use that, instead of what you had, which was "E:\\\\LongDi~1\\\\SecDir01\\\\myfile\\.txt", which is something else again.

    EDIT: Note that the string "E:\LongDi~1\SecDir01" contains the characters E, :, <control character>, o, n, g, etc., while the string "E:\\LongDi~1\\SecDir01" contains the characters E, :, \, L, o, n, g, etc. That is to say the string only has single backslashes in it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mkdir function and errno
    By v333k in forum C Programming
    Replies: 2
    Last Post: 04-30-2010, 05:30 PM
  2. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  3. Replies: 12
    Last Post: 04-25-2007, 02:48 PM
  4. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  5. mkdir and multiple directories
    By Idle in forum C Programming
    Replies: 4
    Last Post: 06-28-2003, 04:56 AM