首页 > 解决方案 > 关于 LoadVector 语法的说明(将 txt 文件转换为向量)

问题描述

我正在做一个项目,但在使用 Loadvector 语法时遇到了问题。到目前为止我有这个

/* ----------------------------------------- */
/* --------------LoadVector ---------------- */
/* ----------------------------------------- */
vector <int> LoadVector(string filename) {
    vector <int> c;

    cout << "Open the file" << endl;
    ifstream cf(filename);

    // test file open
    if (cf.is_open()) {
        int value;

        // read the elements in the file into a vector
        while (cf >> value) c.push_back(value);

        // close the file
        cout << "Close the file" << endl;
        cf.close();
    }
    else {
        cout << "File not found" <<endl;
    }
    return c;
}

当我尝试构建它时,我得到“打开文件未找到文件”

这些文件是一系列数字,我将它们作为 .txt 文档保存在项目文件夹中。我认为我在程序指向获取文件的地方遗漏了一些东西。任何建议将不胜感激。

谢谢!

标签: c++vector

解决方案


推荐阅读