2016-06-23 6 views
1
#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のコードブロックで何をすべきか教えてください!MacでコードブロックをIfstreamするには?

答えて

0

ファイルの絶対パスを絶対パスで記述する必要があります。私は同じ質問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; 
} 
関連する問題