将文件读入数组并从 C++ 函数中 return

Read file into array and return it from a function C++

在Lua中,我有这样一个将文件读入数组的函数:

function readFile(file)
  local output = {}
  local f = io.open(file)
  for each in f:lines() do
    output[#output+1] = each
  end
  f:close()
  return output
end

现在在 C++ 中,我试着这样写:

string * readFile(file) {
  string line;
  static string output[] = {};
  ifstream stream(file);
  while(getline(stream, line)) {
    output[sizeof(output)+1] = line;
  }
  stream.close();
  return output;
}

我知道您不能 return 函数中的数组,只能是指针。所以我这样做了:

string *lines = readFile("stuff.txt");

它给我带来了错误 cannot convert 'std::string {aka std::basic_string<char>} to' std::string* {aka std::basic_string<char>*}' in intialization string *lines = readFile("stuff.txt");

谁能告诉我这里出了什么问题,有没有更好的方法将文件读入数组?

编辑: 我将使用 returned 数组通过 for 循环进行值匹配。在 Lua 中,这将被写为:

for _, each in ipairs(output) do
  if each == (some condition here) then
    --Do Something
  end
end

如何使用向量在 C++ 中完成此操作(根据 Jerry Coffin 的回答)?

编辑 2: 由于某种原因,我无法正确匹配向量。我把代码写在一个单独的测试文件中。

int main() {
  vector<string> stuff = read_pass();
  cout << stuff.size() << endl;
  cout << stuff[0] << endl;
  if (stuff[0] == "admin") { 
    cout << "true"; 
  }
  else { 
    cout << "false"; 
  }
  return 0;
}

read_pass() 看起来像这样:

vector<string> read_pass() {
  ifstream stream("stuff.txt");
  string line;
  vector<string> lines;
  while(getline(stream, line)) {
    lines.push_back(line);
  }
  stream.close();
  return lines;
}

stuff.txt 看起来像这样:

admin
why?
ksfndj

我只是随便放了一些行来测试代码。每次我编译 运行 main.cpp 我得到的输出是

3
admin
false

为什么代码没有正确匹配?

编辑 3:

因此,我没有强迫自己使用向量方法做事,而是决定尝试这样做:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include "basefunc.h"

using namespace std;

int main() { 
  string storedUsrnm;
  string storedPw;
  string pw = "admin";
  string usrnm = "admin";
  ifstream usernames("usrnm.accts");
  ifstream passwords("usrpw.accts");
  while(getline(usernames, storedUsrnm)) {
    getline(passwords, storedPw);
    print("StoredUsrnm " + storedUsrnm);
    print("StoredPw: " + storedPw);
    if (storedUsrnm == usrnm && storedPw == pw) {
      print("True!");
      return 0;
    }
  }
  print("False!");
  return 0;
}

其中 print() 是

void print(string str) {
  cout << str << endl;
}

最后仍然打印 false,这让我相信由于某种原因,ifstream 读取的 "admin""admin" 字符串不同。对这是怎么回事有什么解释吗?或者此代码也不起作用?

在我看来,您当前的代码甚至都不应该编译。不管怎样,我可能会这样做:

std::vector<std::string> read_file(std::istream &infile) { 

    std:string line;
    std::vector<std::string> lines;

    while (std::getline(infile, line))
        lines.push_back(line);

    return lines;
}

所以这里的基本思想是从文件中读取一行,如果成功,将该行(push_back)添加到结果向量中。重复直到从文件中读取一行失败。然后return所有行的向量到调用者。

一些注意事项:尤其是在开始时,可​​以相当安全地假设任何指针的使用都可能是错误的。这不应该被视为指针非常难以使用或类似的东西的迹象——只是对于大多数相对初学者在 C++ 中所做的事情来说,它们几乎从来都不是必需的。

数组也一样——首先,假设您可能认为的某种其他语言中的数组转换为 C++ 中的 std::vector。 C++ 也有数组,但使用它们需要等待一段时间(很长一段时间,IMO——我已经写 C++ 几十年了,实际上从不使用原始指针 or 数组全部)。

为了简单起见,我将数据合并到程序中,因此它从 stringstream 中读取数据,如下所示:

#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

vector<string> read_pass(istream &is) {
    string line;
    vector<string> lines;
    while (getline(is, line)) {
        lines.push_back(line);
    }
    return lines;
}

int main() {
    istringstream input{ "admin\nwhy?\nksfndj" };
    // To read from an external file, change the preceding line to:
    // ifstream input{ "stuff.txt" };

    vector<string> stuff = read_pass(input);
    cout << stuff.size() << endl;
    cout << stuff[0] << endl;
    if (stuff[0] == "admin") {
        cout << "true";
    }
    else {
        cout << "false";
    }
    return 0;
}

至少对我来说,这会产生:

3
admin
true

...表明它已按预期工作。我对外部文件也一样。如果您对外部文件不满意,我的直接猜测是该文件(至少第一行)包含一些您不期望的数据。如果问题仍然存在,您可以考虑以数字格式写出您读取的字符串的各个字符,以便更明确地了解您真正读取的内容。

折腾了半天,终于想出了答案

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <map>

using namespace std;

typedef map<int, string> strArr;

strArr readFile(string file) {
  ifstream stream(file);
  string line;
  strArr output;
  while(getline(stream, line)) {
    output[output.size()+1] = line;
  }
  stream.close();
  return output;
}

它不会将文件读入数组,但它会return一个地图,其功能基本相同