我有
const char *pathname = "..\somepath\somemorepath\somefile.ext";
Run Code Online (Sandbox Code Playgroud)
如何将其转化为
"..\somepath\somemorepath"
Run Code Online (Sandbox Code Playgroud)
?
我想用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)