gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:40:39 25 4
gpt4 key购买 nike

我正在编写一个程序来告诉我是否成功更改了我的目录。然而,这是行不通的。我经历了调试器模式,据我所知,该程序只是查看我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com