基本上,我想为一个更大的项目创建一个简单的函数,这实际上最终将作为一个处理我所有库存的Header文件。任何人,我只是希望它能够从.csv文件格式读取/拉取/输入数据......或者如果更容易使这个功能起作用,我甚至可以做.txt,只要我能在MS Excel中打开并编辑项目并添加新项目,当函数运行时,它将打开“Example.csv”,例如,该函数将查找第1列中的单元格 - 'sSwordName',它将拉取该行中的数据,关闭该单元格,在所有输入中完全排除第一列...它将它组合在一起,或者为了将其分配给该变量,它可能需要做什么。请参阅Code.h(评论)以查看我的源代码问题。
Code.h -
#include "stdafx.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
char sSwordName[100][25] = {};
int sSwordLvlR[100] = {};
vector<string> split_at_commas(const string & row)
{
vector<string> res;
istringstream buf(row);
string s;
while (getline(buf, s, ','))
res.push_back(s);
return res;
system("pause");
}
/* Question 1: Where & How do I properly add 'ifstream wInv("Example.csv");' in order to Load the CSV file it is reading? */
/* Question 2: Line 38 & 46. */
/* Question 3: Line 39 & 47 */
int main()
{
int i = 1;
string line;
string row;
vector<string> values = split_at_commas(line);
if (values[0] == "sSwordName")
{
for(int i = 1; i < values.size(); ++i);
{
/*int i error: Object must have a pointer-to-object type*/
sSwordName[100][25][i - 1] = /*How do I convert string to char?*/(vector[i]);
}
}
else if (values[1] == "sSwordLvlR")
{
for(int i = 1; i < values.size(); ++i);
{
/*int i error: Object must have a pointer-to-object type*/
sSwordLvlR[i - 1] = /*How do I Convert string to int?*/(vector[i]);
}
}
}
/* Question 4: Is there anything else that is wrong in this? If so, how would I fix it */
Example.csv -
sSwordName,Wooden Shortsword,Bronze Shortsword,Iron Shortsword,Steel Shortsword,Titanium Shortsword
sSwordLvlR,1,3,5,6,10
有关CSV的更多信息:
sSwordName,Wooden Shortsword,Bronze Shortsword,Iron Shortsword,Steel Shortsword,Titanium Shortsword
sSwordLvlR,1,3,5,6,10
^不是我可以格式化它的唯一方式,为方便起见我可以做这样的事情(下面,E2.csv)。如果更容易制作功能,请执行。;
sSwordName,"Wooden Shortsword","Bronze Shortsword","Iron Shortsword","Steel Shortsword","Titanium Shortsword"
sSwordLvlR,"1,","3,","5,","6,","10,"
我甚至可以用这种方式格式化它;
sSwordName,"{"Wooden Shortsword","Bronze Shortsword","Iron Shortsword","Steel Shortsword","Titanium Shortsword"};
sSwordLvlR,"{1,3,5,6,10};"
再一次,我非常感谢任何帮助。我在高级中感谢你! -Leaum