【问题标题】:How to Ifstream codeblocks on mac?如何在 Mac 上流式传输代码块?
【发布时间】:2016-06-23 14:33:21
【问题描述】:
#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 上的代码块中做什么来实现这一点!

【问题讨论】:

    标签: c++ macos file-io codeblocks ifstream


    【解决方案1】:

    您必须编写文件的完整绝对路径,而不是相对路径。我已经回答了同样的问题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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-05
      • 2013-10-24
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2010-09-10
      • 2010-09-24
      相关资源
      最近更新 更多