在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;
}
我知道您不能从函数返回数组,只能返回指针。所以我这样做了:
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");
谁能告诉我这里出了什么问题,有没有更好的方法将文件读入数组?
编辑: 我将使用返回的数组通过 for 循环进行值匹配。在 Lua 中,这将被写为:
for _, each in ipairs(output) do
if each == (some condition here) then
--Do Something
end
end
如何使用 vector 在 C++ 中完成此操作(根据 Jerry Coffin 的回答)?
编辑 2: 由于某种原因,我无法正确匹配 vector 。我在单独的测试文件中编写了代码。
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:
因此,我没有强制自己使用 vector 方法,而是决定尝试这样做:
#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;
}
最后仍然打印错误,这让我相信出于某种原因,"admin"
ifstream 读取的内容不同于 "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;
}
所以这里的基本思想是从文件中读取一行,如果成功,将该行添加到结果 vector 中(使用 push_back
)。重复直到从文件中读取一行失败。然后将所有行的 vector 返回给调用者。
一些注意事项:尤其是在开始时,可以相当安全地假设任何指针的使用都可能是错误的。这不应被视为指针非常难以使用或类似的东西的迹象——只是对于大多数相对初学者在 C++ 中所做的事情而言,它们几乎从来都不是必需的。
与数组类似——首先,假设您可能认为在其他语言中的数组在 C++ 中转换为 std::vector
。 C++ 也有数组,但使用它们需要等待一段时间(很长一段时间,IMO——我已经写 C++ 几十年了,实际上根本不使用原始指针或数组)。
为了简单起见,我将数据合并到程序中,因此它从字符串流中读取数据,如下所示:
#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
...表示它已按预期工作。我对外部文件也一样。如果您对外部文件不满意,我的直接猜测是该文件(至少第一行)包含一些您不期望的数据。如果问题仍然存在,您可能会考虑以数字格式写出您读取的字符串的各个字符,以便更明确地了解您真正读取的内容。
关于c++ - 将文件读入数组并从函数 C++ 返回它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28640272/