使用 stdio.h 重新定位和重命名文件C++

Relocating and renaming file C++ using stdio.h

本文关键字:重命名 文件 C++ 定位 stdio 新定位 使用      更新时间:2023-10-16

我正在尝试使用stdio.hrename((函数重命名文件并且它可以工作,但问题是它只能重命名位于当前项目文件夹中的文件,我希望能够选择一个目录,如果可以在此过程中从位置更改它。

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
bool verifier;
char oldName[] = "text.txt";
char newName[] = "newText.txt";
verifier = rename(oldName, newName);
if (!verifier)
{
std::cout << "The file has been succesfully renamedn";
}
else
{
std::cout << "There was a problem renaming the filen";
}
return 0;
}

谢谢!

默认情况下,根目录路径是运行可执行文件的位置。如果你想访问我们之外的另一个文件夹,你可以使用绝对路径(即C:/path/to/old.txt(。

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
char oldName[] = "C:\path\to\your\proj\text.txt"; // char oldName[] = "old.txt";
char newName[] = "C:\test\output\folder\new.txt"; // char newName[] = "newText.txt";
bool verifier = rename(oldName, newName);
if (!verifier)
{
std::cout << "The file has been succesfully renamedn";
}
else
{
std::cout << "There was a problem renaming the filen";
}
return 0;
}