[英]How to Ifstream codeblocks on mac?
#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上的代码块中做些什么来实现这一点!
您必须写入文件的完整绝对路径而不是相对路径。 我在这里回答了同样的问题。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.