相关疑难解决方法(0)

c ++如何从路径字符串中删除文件名

我有

const char *pathname = "..\somepath\somemorepath\somefile.ext";
Run Code Online (Sandbox Code Playgroud)

如何将其转化为

"..\somepath\somemorepath"
Run Code Online (Sandbox Code Playgroud)

c++ filesystems string

24
推荐指数
3
解决办法
4万
查看次数

如何将工作目录更改为程序的位置

我想用c ++在Mac OS上打开一个文件.

如果我在Xcode下运行程序,工作目录与程序相同,这很好.但是,如果我尝试在终端中运行程序,则工作目录始终是"用户/用户名".你知道如何将工作目录更改为程序的位置吗?

以下是示例代码:

#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, const char * argv[])
{
    char * dir = getcwd(NULL, 0); 
    cout << "Current dir: " << dir << endl;

    ifstream fin("hi.txt");
    if (fin.is_open()) cout << "File is Open" << endl;
    else cout << "File is not open" << endl;    
    fin.close();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ macos xcode

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×2

filesystems ×1

macos ×1

string ×1

xcode ×1