5 Replies - 331 Views - Last Post: 12 October 2012 - 06:14 PM Rate Topic: -----

#1 Fidelcashflow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-November 09

C - Moving files.

Posted 12 October 2012 - 02:36 PM

Hi, I have to write a program in C which takes a group of file names as arguments and the last name given is the name of a new directory that must be created. Once the directory is created, the files listed must be moved into the the new directory.

An example run of the program on a linux machine would be like this:
a.out file1.txt file2.avi file3 file4 file5.pdf directory_name


There can be any number of files given. So, we don't know how many to expect.

Being fairly new to C, I am not quite sure how to go about taking in a bunch of names without being given the specific numbers to take in and how to parse the names given so as to detect that the last name is the directory name.

Just looking to be pointed in the right direction on how to begin this.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: C - Moving files.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: C - Moving files.

Posted 12 October 2012 - 02:42 PM

http://www.cprogramm...c/lesson14.html
Was This Post Helpful? 0
  • +
  • -

#3 Fidelcashflow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-November 09

Re: C - Moving files.

Posted 12 October 2012 - 04:09 PM

Thank you for the reply.

I have managed to parse the given commands to the point where I can isolate the directory name as well as the file names. Now the next part where I seem to be a little stuck is, how to go about moving the files. I don't really know how I can actually move the file. I know how to copy a file..just not how to move a file.

Heres my code so far:
#include <stdio.h>

int main(int argc, char* argv[]){
	int i;
	FILE *file;

	/* Create destination folder. */
	mkdir(argv[argc - 1], 0777);

	/* This for loop goes through all of the given arguments.
	 * and gets file names one at a time.
	 */
	for(i = 0; i < argc-1; i++){
		file = fopen(argv[i], "r");
		/* Print statement used for testing. */
		printf("current file = %s\n", argv[i]);
	}

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 hoople  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 06-September 12

Re: C - Moving files.

Posted 12 October 2012 - 04:18 PM

You don't need to open the files, just use the rename(const char* old_filename, const char* new_filename) function from stdio.h. Your task now is to isolate each file name and extension from the folder it's located in, and append it to the directory_name.
Was This Post Helpful? 1
  • +
  • -

#5 Fidelcashflow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 11-November 09

Re: C - Moving files.

Posted 12 October 2012 - 04:25 PM

I am not quite sure how to "append it to the directory_name" is there a function to do that? Or is there another way to do that?...
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: C - Moving files.

Posted 12 October 2012 - 06:14 PM

You can use strcat in <string.h>
char path[100];
strcat(path, "C:/your_path/");
strcat(path, "your_filename.txt");


This post has been edited by jjl: 12 October 2012 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1