#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("test.txt");
int foo;
string sFoo;
inFile >> sFoo;
inFile >> foo;
cout << "the name is " << sFoo << endl;
cout << "the first number is " << foo << endl;
inFile >> foo;
cout << "the second number is " << foo << endl;
cout << "Hello World!";
return 0;
}
我试过将我的文本文件放在同一个文件夹中。但是,由于某种原因,它无法读取文本文件。请有人告诉我在 macbook 上的代码块中要做什么才能实现这一目标!
最佳答案
您必须写下文件的完整绝对路径,而不是相对路径。我已经回答了同样的问题here .
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("/Users/user/Desktop/test.txt");
if(inFile){
int foo;
string sFoo;
inFile >> sFoo;
inFile >> foo;
cout << "the name is " << sFoo << endl;
cout << "the first number is " << foo << endl;
inFile >> foo;
cout << "the second number is " << foo << endl;
cout << "Hello World!";
inFile.close();
}else{
cout<<"unable to open file"<<endl;
}
return 0;
}
关于c++ - 如何在 mac 上使用 Ifstream codeblocks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37994652/