Looping through arguments

Hi, so im trying to implement a program that simulates the linux cp command. My program copies the content of one file to another and also copies a file to a direcotry in the current directory. Below is my main () function that works for these cases

But Im trying to add a loop so that i can copy multiple files to a directory at the same time. The loop should do the following:

- loops through all my arugments ( so from av[1] to av[ac-2])
- copy it to dest
- passes av[i] and av[ac-1] to a function i have called copyFile where i is the loop index.

How do i do this? Im not sure how make a loop to go through all my arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  int main(int ac, char *av[])
{
  /* checks args */
  if(ac != 3 )
  {
    fprintf(stderr, "usage: %s source destination\n", *av);
    exit(1);
  }
 
   char *src = av[1];
   char *dest = av[2];
 
 
   if( src[0] != '/' && dest[0] != '/' )//cp2 file1.txt file2.txt
   {
       copyFile(src, dest);
   }
   else if( src[0] != '/' && dest[0] == '/' )//cp2 file1.txt /dir 
   {
      int i;
     for(i=1; i<=strlen(dest); i++)
      {
          dest[(i-1)] = dest[i];
      }
      strcat(dest, "/");
      strcat(dest, src);
      copyFile(src, dest);
	
   }

  /*
	CHANGE LOOP ABOVE SO THAT I CAN DO THIS cp2 file1.txt file2.txt      file3.txt  /dir


  */

  else
  {
      fprintf(stderr, "usage: cp1 source destination\n");
      exit(1);
  }
}
Something along these lines perhaps (this uses std::experimental::filesystem library; the logic for handling command line arguments would remain the same if we use platform-specific functions instead):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem ;

int main( int argc, char* argv[] )
{
    try
    {
        // copy a single file or copy a directory
        if( argc == 3 ) fs::copy( argv[1], argv[2], fs::copy_options::recursive ) ;

        else if( argc > 3 ) // copy many files to a directory
        {
            const int dest = argc - 1 ;
            fs::path dest_path = argv[dest] ;
            while( fs::is_symlink(dest_path) ) dest_path = fs::read_symlink(dest_path) ;

            if( !fs::is_directory(dest_path) )
            {
                std::cerr << "error: destination path is not a directory\n" ;
                return 1 ;
            }

            for( int i = 1 ; i < dest ; ++i ) fs::copy( argv[i], dest_path ) ;
        }
         
        // else (argc < 3) invalid command line. show usage etc.
    }

    catch( const fs::filesystem_error& err )
    {
        std::cerr << "filesystem error:\n\tsrce: " << err.path1()
                  << "\n\tdest: " << err.path2() << "\n\t what: " << err.what() << '\n' ;
        return 1 ;
    }

    catch( const std::exception& err )
    {
        std::cerr << "error: " << err.what() << '\n' ;
        return 1 ;
    }
}
Topic archived. No new replies allowed.