Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
So for this I have to copy a file or folder from destination to source and then erase the source file or folder. I don't know how to copy physical a file or folder.

This is my code, but I need another method, can you help me please ?

I need the function mv -i, -t, -s

What I have tried:

void mv_i()
{
  int mutat;
  char *mvFile_name1;
  char *mvFile_name2;
  char answerMv[100];

  mvFile_name1=params[2];
  mvFile_name2=params[3];

  printf("Are you sure you want to move %s into %s?", mvFile_name1, mvFile_name2);
  scanf("%s", answerMv);

  if(answerMv[0] == 'y')
  {
    mutat = rename(mvFile_name1, mvFile_name2);
    if(mutat != 0)
    {
      perror("Error");
    }
  }
}

void mv_files()
{
  int mutat;
  char mvFile_name1[30];
  char mvFile_name2[30];

  printf("The file you want to move: ");
  //gets(mvFile_name1);
  scanf("%s", mvFile_name1);
  printf("The location file: ");
  //gets(mvFile_name2);
  scanf("%s", mvFile_name2);

  mutat = rename(mvFile_name1, mvFile_name2);
  if(mutat != 0)
    perror("Error");
}

void mv_t()
{
  int mutat;
  char mvFile_name1[256];
  char mvFile_name2[256];
  char *locatie1="";
  char *locatie2="";
  int nr=0;

  locatie1=malloc(sizeof(char*));
  locatie2=malloc(sizeof(char*));

  printf("How many files you want to move: ");
  scanf("%d", &nr);

  printf("The file where you move: ");
  scanf("%s", mvFile_name2);

  strcpy(locatie2, mvFile_name2);

  for(i=0; i<nr;>  {
    printf("The file you want to move: ");
    scanf("%s", mvFile_name1);

    strcpy(locatie1, mvFile_name1);

    mutat = rename(locatie2, locatie1);

    if(mutat != 0)
      perror("Error");
  }
}
Posted
Updated 15-Feb-16 8:11am
v2
Comments
Sergey Alexandrovich Kryukov 15-Feb-16 14:25pm    
Before anyone looks into your code, please, why would you need "another method"? What for? What's the problem?
—SA

1 solution

There are many C code examples on copying a file when searching the web.

An example is fileio/copy.c (from "The Linux Programming Interface")[^].

Various methods are discussed in the thread How can I copy a file on Unix using C? - Stack Overflow[^].

Once you have implemented the copy function, write a move function that calls the copy function and deletes the source file when copying was successful.

The final step is creating the full source and destination file names according to the options and calling the move function for each file. To implement this you might need these library functions:

where stat or access can be used to check for file existance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900