By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

Something Wrong

dfound
P: 52
What is wrong with the foll. code. Let me tell the problem first.I intended to use this code to delete folders completely(i am using Turbo C++). The no.of folders depend up on users input(argc).Oklet me tell the problem .The problem is that the code when executed deletes the folders given by the user(that is what I want) but it also deletes all the files in the folder in which it is kept including itself.I think the problem is with chdir() but I don't know how to correct itPlease help me... :-)
Expand|Select|Wrap|Line Numbers
  1. for( nof=0;nof<(argc-2);nof++)
  2.       {
  3.  
  4.         chdir(argv[nof+1]);
  5.         done=findfirst("*.*",&ffblk,0);
  6.         while(!done)
  7.         {
  8.          unlink(ffblk.ff_name);
  9.          done=findnext(&ffblk);
  10.          }
  11.          if(nof==argc-3)
  12.          {
  13.          d=getdisk();
  14.          if(d<3)
  15.          setdisk(d+1);
  16.          else
  17.          setdisk(d-1);
  18.          }
  19.          else
  20.              chdir(argv[nof+2]);
  21.  
  22.          rmdir(argv[nof+1]);
  23.  
  24.         if(nof==(argc-3))
  25.         break;
  26.        }
  27.        textcolor(10);
  28.        cprintf("\n\rDELETION SUCCESSFUL");
  29.        cprintf("\n\rPress any key to continue...");
  30.        getch();
  31.        exit(0);
  32.  
HELP ME........
Jan 26 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
you code lacks comments so it is difficult to figure out what it is doing - in particular could you explain what each of the argv[] parameters is
Jan 26 '07 #2

dfound
P: 52
I am very very sorry to trouble you.Yesterday night , I sat up trying so many ways to figure how I can do this and finally,I found a way.Any way, I am very thankful to you for your reply.One more thing, do you know hot to use the chdir()
command.Whenever I use it, it doenot change the directory.The directory stays the same.I will be greatful to you if you help me with this. The code I am using now for search is

this is a commandline program.So the input is something like this

fe *.* d: s

fe is name of program(short for file encoder)

*.* is name of file to be searched

d: is directory in which file is to be searched

s is for search


Expand|Select|Wrap|Line Numbers
  1. if(argc>4||argc<4)             //as 4 arguments are needed ,if it is not so,an       
  2.      {                   //error is printed
  3.  
  4.      textcolor(12);
  5.      cprintf("\n\rWRONG NUMBER OF PARAMETERS FOR SEARCH");
  6.      cprintf("\n\rPress any key to exit...");
  7.      getch();
  8.      exit(0);
  9.  
  10.      }
  11.      strcpy(sl,argv[2]);       // sl is a array.argv[2] is directory name
  12.      strcat(sl,"\\");            //so it is now sl is now d:\\
  13.      strcat(sl,argv[1]);       //argv[1] is file to be searched Now,sl
  14.      textcolor(10);            //is d:\*.*
  15.      cprintf("\n\rSEARCH RESULTS");
  16.      done=findfirst(sl,&ffblk,0);        //this is usual search using findfirst()
  17.      while(!done)
  18.      {
  19.      for(i=0;i<15;i++)         //the for loop is used to print only 15 search
  20.      {                                //results at a time
  21.      textcolor(LIGHTMAGENTA);
  22.       no++;
  23.      cprintf("\n\r%s",ffblk.ff_name);   //printing filenames
  24.       done=findnext(&ffblk);
  25.      if(done)
  26.      break;
  27.      }
  28.      if(done)
  29.      break;
  30.      getch();
  31.      clrscr();
  32.      textcolor(14);
  33.      cprintf("\n\r                      \t\t  FE 2.0 C -by- RAHUL.ES   \t\t\n\n\r");
  34.      textcolor(10);
  35.          cprintf("\n\rSEARCH RESULTS");
  36.  
  37.  
  38.      }
  39.  
  40.  
  41.      textcolor(10);
  42.      cprintf("\n\n\rSEARCH SUCCESSFUL.");
  43.      cprintf("\n\rPress any key to exit...");
  44.      getch();
  45.      exit(0);
  46.  
Jan 28 '07 #3

Expert 100+
P: 1,510
don't see why chdir() - change the process's current working directory - will not work if the target directory exists, e.g. if I have a directory testdir
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <dirent.h>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     system("dir *.*");
  8.     cout << chdir("testdir") << endl;
  9.     system("dir *.*");
  10.     cin.get();
  11. }
  12.  
this lists the files in the current directory, changes to testdir (chdir() return 0 for sucess -1 for fail) and then list the files in testdir

I think you need to print out the value returned by chdir(), rmdir(), etc so you can make sure the commands are working
Jan 28 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.