C++ 使用升华文本2在Mac上使用fstream的相对路径
好的,我的代码很简单C++ 使用升华文本2在Mac上使用fstream的相对路径,c++,macos,sublimetext2,C++,Macos,Sublimetext2,好的,我的代码很简单 #include <iostream> #include <fstream> #include <unistd.h> using namespace std; int main(){ char a; char mas[5000]; fstream fin; fin.open("input.txt", ios::in); if(!fin.is_open()) cout << "No file f
#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;
int main(){
char a; char mas[5000];
fstream fin;
fin.open("input.txt", ios::in);
if(!fin.is_open()) cout << "No file found!";
getcwd(mas, 5000);
cout << mas;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符a;字符mas[5000];
流鳍;
打开(“input.txt”,ios::in);
如果(!fin.is_open())无法则main()
的argv
参数是用于调用可执行文件的命令行,因此您可以使用argv[0]
获取可执行文件的路径,以便构建到输入文件的路径:
#include <string>
#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;
int main(int argc, const char **argv){
char a; char mas[5000];
fstream fin;
string dirname, inputFilename;
const char *p = strrchr(argv[0], '/');
if (p != 0)
dirname = string(argv[0], p - argv[0] + 1);
inputFilename = dirname + "input.txt";
fin.open(inputFilename.c_str(), ios::in);
if(!fin.is_open()) {
cout << "No file found!" << endl;
return 1;
}
getcwd(mas, 5000);
cout << mas << endl;
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,常量字符**argv){
字符a;字符mas[5000];
流鳍;
字符串dirname,inputFilename;
常量char*p=strrchr(argv[0],'/');
如果(p!=0)
dirname=string(argv[0],p-argv[0]+1);
inputFilename=dirname+“input.txt”;
打开(inputFilename.c_str(),ios::in);
如果(!fin.is_open()){
库特