在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
如何使用向量在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;
}
最后,这仍然打印错误,这让我相信由于某种原因,ifstream读取的"admin"
与"admin"
字符串不同。对此如何解释?或者这段代码也不起作用?
答案 0 :(得分:1)
我不相信你当前的代码应该编译。无论如何,我可能会这样做:
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
)添加到结果向量中。重复直到从文件中读取一行失败。然后将所有行的向量返回给调用者。
一些注意事项:特别是首先,假设使用指针可能是一个错误,这是相当安全的。这不应该被视为指针非常难以使用,或类似的东西 - 只是他们几乎不需要大多数相对初学者在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
...表明它已按预期工作。我对外部文件也一样。如果您对外部文件的反应不同,我立即猜测该文件(至少在第一行)包含您不期望的某些数据。如果问题仍然存在,您可以考虑写出您以数字格式阅读的字符串的各个字符,以更清楚地了解您真正阅读的内容。
答案 1 :(得分:0)
很长一段时间后,我终于想出了答案
#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;
}
它不会将文件读入数组,但它会返回一个基本相同的地图