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

Relocating and renaming file C++ using stdio.h

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

#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 renamed\n";
    }
    else
    {
        std::cout << "There was a problem renaming the file\n";
    }

    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 renamed\n";
   }
   else
   {
      std::cout << "There was a problem renaming the file\n";
   }
   return 0;
}