Simple C Program - Copying Files

Soldato
Joined
22 Oct 2005
Posts
2,884
Location
Moving...
Hi all.

My problem is that I want a way to take a playlist file(.m3u), and copy all the songs in this playlist to my desktop.

I've managed to open the file, strip out the rubbish, and be left with a list of file names e.g. "F:/Music/ABC.mp3".

All I need to do then is copy this file into a folder on the desktop, but I can't work out how to do it.

If it was a .txt file I'd just open it up and copy it line by line to the new location, but I don't think I can do it this way here. Therefore I was looking for a function that will allow me to copy the file as a whole. I found CopyFile(), but I can't get it to work, even when I hard code the names in like below nothing is copied.

Code:
CopyFile("C:/Documents and Settings/M/Desktop/test.mp3", 
	"C:/Documents and Settings/M/Desktop/tempMusic", 
	0);


It doesn't even work with a .txt file so I don't think it's the file extension causing issues.

Any idea what I'm doing wrong?

I'm open to using any other methods btw, don't have to use this function, I'm happy to use anything quick and dirty if needs be!

Thanks for any help.
 
Yea just straight C. Just because it's the language I know best (obviously still a noob though!). Could try use something else though if need be, I don't really know any other languages though.

Just tried it with filenames without spaces and still no success.
 
Try:

.....

If that doesn't work, call GetLastError() and check the result to see why its failing.

Still no luck. I'm not sure exactly how to use it but I tried calling the the following directly after the CopyFile() call:

Code:
DWORD dw = GetLastError();

That returns a value of 5.....sorry I have no idea what that means:confused:.
I saw some sample error codes but they were all minus numbers???




**edit** Ahh, does 5 have something to do with access rights?
Just seen a couple of examples that seem to indicate this.

How would I go about getting round this? I'm on winXP, I don't have any restrictions setup that I know of....
 
Last edited:
That maybe to do with NTFS permissions on the folder. Make sure the user account that executes the C code has Modify and write permissions to where your copying. If its the desktop then that will be in your user settings folder.

Sorry for being a simpleton, but how would I do that?

I've even tried copying something from within the program directory so surely it must have permissions in there?

And if it makes a difference, I will actually be copying from another NTFS hard drive onto the desktop, but like I say, I don't think I have any restrictions in place for anything.
 
The documentation on CopyFile (the MSDN page is the first result on Google) says that the second parameter should be the destination/new file name. You're providing a directory, so it fails.

Ah, that's it. If I specify the filename it works fine.

I'll just have to chop up the filename so I can specify an exact name for the new .mp3 file, rather than just specificy the destination folder.

Haven't done it yet but that should be fine!

Thanks for all your help.
 
Arrggghh I suck at programming!!

I'm so close. I've got the following code: (Yes I know it's horrible, I told you I'm a noob!)

Code:
#include <stdio.h> 
#include <string.h>
#include <windows.h>

char* c;
char str[250];
char destStr[250];  
int strLength;
bool endOfFile = false;
bool foundSlash = false;
bool foundLF = false;
int i, j, x;



int main () {

	FILE *inFile; 
	inFile = fopen("F:/My Music/Playlist.m3u","r"); 

	while (endOfFile == false){

		c = fgets(str, 250, inFile);

		if (c == NULL){ //Found end of file so break 
			break;
		}

		else {
			if ( str[0] == '#'){  
				//Do Nothing becasue it's rubbish
			}
			else{

				//Remove line feed from end of string
				foundLF = false;
				x = 0;
				while (!foundLF){
					if (str[x] == 10) {
						str[x] = NULL;
						foundLF = true;
					}
					else
						x++;

				}

				strLength = strlen(str);
				strLength = strLength ;	//  Minus 1 to go to array position [0], 
											

				foundSlash = false;
				i = strLength;
				j = 0;

				while (!foundSlash){ //Look for first backslash from the right, stuff after this slash is the filename

					if (str[i] == 92){ //92 = ascii value of backslash
						//Create destination filename

						while (i<=strLength){
							destStr[j] = str[i+1];  //Plus 1 as we don't want to include the backslash in the filename
							i++;
							j++;
						}

						//Copy the file
						destStr = strcat("C:/Documents and Settings/M/Desktop/tempMusic/", destStr); 
						CopyFile(str, destStr, 0);

						foundSlash = true;
					}

					else {
						//Keep searching
						i--;
					}
				}
			}
		}
	}

	fclose(inFile);

	return 0;
}


It's the line:

Code:
destStr = strcat("C:/Documents and Settings/M/Desktop/tempMusic/", destStr);

That's causing me the problem. What I'm trying to do is simply put in a location for it to put the mp3's in, so it's an absolute path. The program will run without this line, but it just saves the mp3's it in the program directory.

The line is telling me I "cannot convert from 'const char [52]' to 'char []'". I kind of understand why, I just don't know how to get around it! Any tips?

Thanks.
 
Back
Top Bottom