c++ - _chdir() 不改变目录 C++

标签 c++ directory

我正在编写一个程序来告诉我是否成功更改了我的目录。然而,这是行不通的。我经历了调试器模式,据我所知,该程序只是查看我的 Visual Studio 项目文件夹 c:\Users#'s\blablabla 中的文件 ----- 我想找到 dir1,它是我桌面上的一个文件夹(这个目录可以在任何地方) 这是我的代码

#include <iostream>
#include <fstream>
#include <direct.h>
#include <io.h>
#include <cstring>
#include "functions.h"

using namespace std;


int main(int argc, char**argv)
    {

        int i = 1;
        if (argc < 2)
        {
            cout << "no command line arguments given" << endl;

        }


for (i = 1; i < argc; i++)
{


    if (_chdir(argv[i]) == 0)
    {
        cout << "Program changed directories successfully" << endl;

    }
    else
        cout << "Unable to change to the directory " << argv[i] << endl;

 }
}

最佳答案

I want to find dir1 which is a folder on my desktop (and this dir could be anywhere)

_chdir 函数不会在您的整个计算机上搜索目录。它准确地更改为您指定的目录。如果您传递 c:\\dir1,则它会尝试更改到目录 c:\\dir1:根目录中的 dir1 目录你的 C: 驱动器。如果要搜索计算机上名为 dir1 的任何目录,您需要自己执行搜索。

关于c++ - _chdir() 不改变目录 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26045152/

相关文章:

C++ 错误处理——示例代码的良好来源?

c++ - OSVERSIONINFOEX 中的最后内部版本号

linux - 用于删除所有子目录中文件的 Bash 脚本。

directory - Puppet 同步目录

c++ - 这段代码是什么意思?打开简历

c++ - QWT 表盘,显示单位

path - 如何使用 .lua 导航到上面的目录

C++ 编程 : Navigating to user files to save my application data for each user

c++ - std::map、多态和删除

python - 如何在 Python3 中递归删除空文件夹?