天天看点

C++修改文件名

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012750702/article/details/53326521

windows 及 ubuntu下均验证成功

很容易,一个函数就搞定了,

rename(oldName.c_str(), newName.c_str())           

此函数带返回值,0为成功,1为失败。

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. int main(int argc, char *argv[])
  5. {
  6. std:: string oldName, newName;
  7. #ifdef _WIN32
  8. oldName = "F:\\data\\test\\old.jpg";
  9. newName = "F:\\data\\test\\new.jpg";
  10. #else
  11. oldName = "/media/myUbuntu/F/data/test/old.jpg";
  12. newName = "/media/myUbuntu/F/data/test/new.jpg";
  13. #endif
  14. if (!rename(oldName.c_str(), newName.c_str()))
  15. {
  16. std:: cout << "rename success "<< std:: endl;
  17. }
  18. else
  19. {
  20. std:: cout << "rename error "<< std:: endl;
  21. }
  22. return ;
  23. }