1

I have a list of file names in a .txt document, and I would like to move each of these files from one folder to another. Using MoveFileA() I am getting the error, "no suitable conversion between std::string and LCPSTR". Here is my code, after opening up my .txt file:

while (std::getline(myfile, line))
{
std::string oldLocation = "C:\\Users\\name\\Desktop\\docs\\folder1\\" + line;
std::string newLocation = "C:\\Users\\name\\Desktop\\docs\\folder2\\" + line;

MoveFileA(oldLocation, newLocation);
}

If I type in the full path as arguments for MoveFileA, instead of sending it a variable, it works but I am unable to iterate over .txt file this way.

Any suggestions on how I might fix this?

user3468711
  • 143
  • 9

1 Answers1

4

LCPSTR means long constant pointer to a string, which means it's a null terminated c string.

std::string is an object. It is something different. But it luckily provides a convenience method c_str the provides a pointer to a constant c style string. So as the comment says you should go by:

MoveFileA(oldLocation.c_str(), newLocation.c_str());

It is worth of explicitly noting, that you can't drop it in every place instead of char*, but only when the string won't be modified. It returns const char*. This is where the C in LCPSTR gets important.

luk32
  • 15,812
  • 38
  • 62