C++ Mac上的代码块未读取文件 #包括 #包括 使用名称空间std; int main() { INTA; ifstream文件(“file.txt”);//文件中只有一个“8” 文件>>A; cout

C++ Mac上的代码块未读取文件 #包括 #包括 使用名称空间std; int main() { INTA; ifstream文件(“file.txt”);//文件中只有一个“8” 文件>>A; cout,c++,macos,codeblocks,fstream,ifstream,C++,Macos,Codeblocks,Fstream,Ifstream,不要更改执行工作目录..当您从文件读取时,尝试写入该文件所在的完整目录,例如: #include <iostream> #include <fstream> using namespace std; int main() { int A; ifstream file("file.txt"); // there is a single "8" in the file file >> A; cout << A; //

不要更改执行工作目录..当您从文件读取时,尝试写入该文件所在的完整目录,例如:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int A;
    ifstream file("file.txt"); // there is a single "8" in the file
    file >> A;
    cout << A; // I get 0 always


    return 0;
}
然后运行一个程序。
如果仍然不起作用,请尝试观看本教程:

我以前读过,我们应该在build target properties中设置执行目录,请看一看。
// this is your file.txt location
ifstream file("C:\\Desktop\\file.txt"); // this is for Windows