【问题标题】:how to go to a specific folder in c++ with a specific address如何使用特定地址转到c ++中的特定文件夹
【发布时间】:2018-05-24 18:01:30
【问题描述】:

我需要转到 C++ 文件中的特定地址并在其中创建文件夹。

这是文件夹地址

C:\Users\218418\Documents\Visual Studio 2013\Projects\Netwokring Application\Netwokring Application\CENTRAL

(我知道我拼错了 Networking,不要评判我)

【问题讨论】:

标签: c++ file directory


【解决方案1】:

这确实会在给定目录中创建一个文件夹。我相信你想在你的给定路径中创建目录。

程序检查目录是否存在,如果不存在则创建它。 (鉴于它有权这样做。)

#include <Windows.h>
#include <string>

void create_folder(const char * path)
{   
    if(!CreateDirectory(path , nullptr))
    {}
}

int main()
{
    const std::string folder_name = "test_folder";
    const std::string base_path =
        "C:\\Users\\218418\\Documents\\Visual Studio 2013\\Projects\\Netwokring Application\\Netwokring Application\\CENTRAL\\";
    std::string result = base_path + folder_name;
    create_folder(result.c_str());
}

【讨论】:

【解决方案2】:

我猜你想在运行时创建/打开一个文件并读/写它。

Files and Streams

例如:

#include <iostream>
ifstream file("your_file.txt");

将打开一个流 file,它可以从您工作目录中的 your_file.txt 中读取。 (保存已编译的 .exe 的位置)

现在,您可以这样使用特定的文件路径:

char *path="C:\Windows\your_file.txt";
std::ofstream file(path);

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多