c++ - 如何在 mac 上使用 Ifstream codeblocks?

标签 c++ macos file-io codeblocks ifstream

#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 上的代码块中要做什么才能实现这一目标!

最佳答案

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

关于c++ - 如何在 mac 上使用 Ifstream codeblocks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37994652/

相关文章:

c++ - JNI : C++ platform in VS2010

windows - 如何为 ReadFileEx() 的完成例程提供上下文信息?

swift - 如何在 Swift MacOS 中获取按钮的 Action

java - 在字符缓冲区的情况下,Java 中的 reader 内部如何工作?

c++ - 尝试从输入文件打印但打印最后一个输入两次

c++ - 检查#include 是否已经声明

c++ - 如果在此调用之前未创建 abcd,那么 `std::map<..> a; blah = a[abcd];` 线程安全吗?

java - 为什么重载与 Java 中的继承一起工作?

ruby - macOS 无法安装 Jekyll Gem

android - Mac 上的 adb 无法检测到 Moto G 设备