简体   繁体   English

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

[英]Relocating and renaming file C++ using stdio.h

I am trying to rename a file with the rename() function of stdio.h and it works but the problem is that it can only rename files located in the folder of the current project, I would like to be able to select a directory and if it is possible to change it from location in the process. 我正在尝试使用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 renamed\n";
    }
    else
    {
        std::cout << "There was a problem renaming the file\n";
    }

    return 0;
}

Thank you! 谢谢!

By default, the root directory path is the location which the executable is running in. If you want to access another folder above our outside that location, you can use an absolute path (ie C:/path/to/old.txt). 默认情况下,根目录路径是可执行文件在其中运行的位置。如果要访问该位置之外的其他文件夹,则可以使用绝对路径(即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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM