Hey guys, I have a question. I'm writing a script in c++ and want to have the script copy a file whose name was previously determined by the script to a output file who's name is determined through functions and procedures within the script. The whole script is looped until it is finished copying, but that's besides the point.

So for example, the script determines that the file name to be copied is "asdf.txt"
I cannot use

CopyFile(asdf.txt, asdf2.txt)

because this process cannot be looped. (Because it will just keep on copying the same file to the same destination over and over.) In other words, is it possible to place variables inside the CopyFile() function? Or is there another way to do this?

KC

Recommended Answers

All 6 Replies

Sure it is:

while(1)
{
string fileName1 = "A_asdf.txt";
string fileName2 = "A_asdf2.txt";
CopyFile(fileName1, fileName2);
fileName2 = fileName1;
fileName1.replace(1,1,fileName[0]++);
}

Ok, I understand line 3 through 5, but what do line 1, 6 and 7 do?

error on line 14: cannot convert 'std::string' to 'const CHAR*' for argument '1' to 'BOOL CopyFileA(const CHAR*, const CHAR*, BOOL)'

error on line 16:'fileName' was not declared in this scope

#include <iostream>
#include <fstream>
#include <cmath>
#include <ctime>
#include <windows.h>

using namespace std;

int main() {

	while (1) {
		string fileName1 = "A_asdf.txt";
		string fileName2 = "A_asdf2.txt";
		CopyFile(fileName1, fileName2);
		fileName2 = fileName1;
		fileName1.replace(1, 1, fileName[0]++);
	}

	return 0;
}
fileName1.c_str()

1. while(1) : Infinite Loop
2. 6-7 Cycles the fileNames after copying.

fileName1.c_str()

1. while(1) : Infinite Loop
2. 6-7 Cycles the fileNames after copying.

ok. But i'm getting errors (look at the post above). I think the error is saying that it has to be a const char* inside the copyfile() function. I'm not sure though, could you look it over?

also, what does

fileName1.c_str();

do? where does it go? Sorry, I've only started programming in c++ recently.

.c_str() is a method of std::string to return the string as a C-style char string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.