[英]How to rename a file with a “unknown” name in C++?
A VM creates a file and a .vbs gets it's directory and name. VM创建一个文件,.vbs获取它的目录和名称。 Simply by checking for .m4a files in a directory. 只需检查目录中的.m4a文件即可。 (there is only one at a time) and I'd like to rename the file, It says there is no such file or directory though. (一次只有一个),我想重命名该文件,它说虽然没有这样的文件或目录。
ifstream infile;
infile.open("A:\\Spotify\\Sidifyindex\\indexchecker.txt");
The file says "Z:\\Spotify\\Sidify test out\\01 VVS.m4a" 该文件显示为“ Z:\\ Spotify \\ Sidify测试输出\\ 01 VVS.m4a”
getline(infile, VMin);
infile >> VMin;
infile.close();
//clear drive letter
VMin.erase(0, 1);
//add new drive letter
VMin = "A" + VMin;
//copy file dir
string outpath;
outpath = VMin;
//get new file name
outpath.erase(0, 30);
outpath = "A:\\Spotify\\Sidify test out\\" + outpath;
//convert to const char*
const char * c = VMin.c_str();
const char * d = outpath.c_str();
//rename
int result;
char oldname[] = "VMin.c_str()";
char newname[] = "outpath.c_str()";
result = rename(oldname, newname);
if (result == 0)
puts("File successfully renamed");
else
perror("Error renaming file");
cout << VMin << endl;
cout << outpath << endl;
I'm getting "Error remaining file: no such file or directory" Output is correct "A:\\Spotify\\Sidify test out\\01 VVS.m4a" and "A:\\Spotify\\Sidify test out\\VVS.m4a" 我收到“错误的剩余文件:没有此类文件或目录”的输出是正确的“ A:\\ Spotify \\ Sidify测试输出\\ 01 VVS.m4a”和“ A:\\ Spotify \\ Sidify测试输出\\ VVS.m4a”
I assume that the issue is hidden somewhere in the rename part 我认为问题隐藏在重命名部分的某处
You wrote: 你写了:
char oldname[] = "VMin.c_str()";
char newname[] = "outpath.c_str()";
but you probably meant to do: 但您可能打算这样做:
char oldname* = VMin.c_str();
char newname* = outpath.c_str();
The first variant will look for a file which is called "VMin.c_str()" which does not exist and thus you are getting this error. 第一个变体将查找一个不存在的名为“ VMin.c_str()”的文件,因此会出现此错误。 You accidentally have put C++ code into quotes. 您不小心将C ++代码放在引号中。 Quotes are only for verbatim strings, like messages and fixed filenames. 引号仅用于逐字字符串,例如消息和固定的文件名。 But your filenames are determined programmatically. 但是您的文件名是通过程序确定的。
You can use the const char *
c
and d
you are calculating above and pass these to rename()
. 您可以使用上面正在计算的const char *
c
和d
,并将它们传递给rename()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.