【问题标题】:Open and read a file from outside of project VS2013从项目 VS2013 外部打开并读取文件
【发布时间】:2015-02-08 14:39:39
【问题描述】:

我无法从项目外部读写文件。添加现有项目时没有链接选项,请帮助。

【问题讨论】:

    标签: c++ c visual-studio


    【解决方案1】:

    您的意思是要将文件添加为对项目的引用,还是要使用代码修改文件中的数据?

    如果你想添加文件并在你的项目中引用它,你应该可以通过右键单击项目名称来做到这一点,然后

    Properties->Common Properties->References->Add New Reference->Browse

    然后找到你要引用的文件。

    对于文件的读取和写入,您可以分别使用#include <fstream>std::ifstreamstd::ofstream 进行输入和输出。

    你可以这样做:

    #include <fstream>
    #include <string>
    #include <iostream>
    
    using std::ifstream;
    using std::ofstream;
    using std::string;
    using std::cout;
    using std::endl;
    
    void main() {
        string file = "C:\\Users\\Foo\\Desktop\\Bar.txt";
    
        ofstream output(file);
        output << "Hello World" << endl;
        output.close();
    
        ifstream input(file);
        string inputStr;
        while(input >> inputStr) {
            cout << inputStr << endl;
        }
    
        return;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 2011-05-11
      • 2019-07-11
      • 1970-01-01
      相关资源
      最近更新 更多