美文网首页
Xcode 读取代码所在目录txt或文件

Xcode 读取代码所在目录txt或文件

作者: kakukeme | 来源:发表于2017-04-10 13:52 被阅读1156次

参考链接: http://blog.csdn.net/u010053344/article/details/51379222

前言

Xcode 写C++ 代码时候,会碰到读取本目录下txt文件的内容,但是又不想使用绝对路径(ps:Mac 绝对路径真是太长了),那么就是得使用相对路径了。可以做如下设置,就可以打开文件并读取了

Step 1:

  • 打开xcode
  • 点击Product
  • 点击Scheme目录
  • 点击Edit Scheme
  • 进入Step 2
这里写图片描述

Step 2

在左边选择Run选项, 然后在右边选项框里面,上面一行选项中选择Option, 然后在Working Dictionary选项里面,打钩 Using cusom working dictionary,经测试成功

这里写图片描述

Step 3 自测结果

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    string fileName = "test.txt";
    
    ifstream file(fileName);    // fileName内容读取到file中
    //file.open(fileName);
    string line;
    
    assert(file.is_open()); // 确定文件打开了;
    
    getline(file, line);    // 读取文件第一行,并输出
    
    cout<< line << endl;
    
    return 0;
}
c++.png

相关文章

网友评论

      本文标题:Xcode 读取代码所在目录txt或文件

      本文链接:https://www.haomeiwen.com/subject/bbusattx.html