2016-06-23 17 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; 
} 

Ich habe versucht, meine Textdatei in den gleichen Ordner zu setzen. Aus irgendeinem Grund ist es jedoch nicht in der Lage, die Textdatei zu lesen. Bitte kann mir jemand sagen, was ich in codeblocks auf Macbook machen soll, damit dies geschieht!Wie Ifstream Codeblocks auf Mac?

Antwort

0

Sie müssen den vollständigen absoluten Pfad Ihrer Datei schreiben und nicht relativ. Ich habe die gleiche Frage here beantwortet.

#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; 
}