Visual Studio C ++将CSV文件中的数据输入到变量数组中

时间:2011-08-27 09:19:47

标签: c++ arrays visual-studio-2010 variables csv

我一直试图将我的大项目的这一小部分想象一段时间。它也让我很难过......我正在尝试从Excel CSV文件中获取信息(参见Example.CSV)并将其输入到Cpp项目中。现在,在我开始研究之前,我一直在努力,我想我可能会先做一些其他的方法,看看你认为最有效的方法吗?

1:Example1.csv:Item值仍然跟随; sSwordName, lvlReq, minAtk, maxAtk, atkRate, sPrice, sValue, strReq, atkReq, intReq。列标题如Example.csv中所示..当被告知时,像标题一样从CSV文件中提取项目信息是否理想,例如:如果玩家在商店,他想评价他的剑。当他点击值按钮时,它会在'Sword.csv'文件中搜索商品ID(可能),并返回'5th',因为该商品行中的第5列的值是它的值。只是创建某种功能,为所有事情做到这一点?如果是这样,任何想法如何做到。

2:我一直试图让这个工作的主要方式: 在Example.csv中,我有我在程序中使用的所有项目值。它们被分配给int和char变量,至少这是我的目标..请直接到Example.cpp获取我的(失败)代码和解释.. < / p>

Example.cpp:

    #include "stdafx.h"
    #include <cstdlib>
    #include <fstream>
    #include <ios>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <sstream>

    using namespace std;

    /*** These are the Variables I use for a weapon, they corrospond according to number in list: Wooden Shortsword is first in the
     *** sSwordName Array, so it's lvl requirment would be 1 since it is first.
     *** Idealy this is the way I want things to work... Unless anyone has a better idea, I am open to hear them. I can always
     *** learn more, especially from others. ***/
    char sSwordName[10][25] =   {"Wooden Shortsword", "Bronze Shortsword", "Iron Shortsword", "Steel Shortsword", "Titanium Shortsword"};
    int sSwordLvlR[10] =    {1, 3, 5, 6, 10};
    int sSwordV[10] =       {5, 10, 18, 25, 50};
    int sSwordP[10] =       {10, 20, 40, 60, 100};
    int sSwordMinAtk[10] =  {0, 0, 0, 0, 0};
    int sSwordMaxAtk[10] =  {4, 6, 10, 14, 20};
    int sSwordAtkRate[10] = {0, 2, 3, 4, 6};
    int sSwordStrR[10] =    {1, 6, 11, 16, 22};
    int sSwordAtkR[10] =    {1, 6, 11, 16, 22};
    int sSwordIntR[10] =    {1, 3, 5, 8, 12};

    /*** Now this is as close as I have been able to get to get this to work, sadly it is still off base...
     *** I need it to take for example: Row 1 > Exclude Column 1 > Input Data > Assign to sSwordName[5][25] Array.
     *** My issue is, finding a way to loop it where it takes all the cells in Row 1, ignores the first cell 
     *** (I think making it do like "\n" for it would work? Not sure) and then loop through the rest of the rows
     *** repeating the same algorithm, Ignoring the first cell, inputting, assinging them to the variable arrays?
     *** It is much much easier to edit item stats and add new items in Excel, than having to do it via code as you
     *** may imagine..
     *** I am pretty sure my code is far from what would be best, I bet it needs to be re-written a new way as well..
     *** I would greatly appreciate Anyone who can help me accomplish this task.. It would really make my life
     *** A lot easier, as well as make my project code considerbly shorter.. */

    int main()
{
    int sSwordLvlR;
    double y;
    string data;
    string a;

    ifstream wInv("Example.csv");
    while (getline(wInv, data))
    {
        if (data.empty()) continue;
        istringstream ss( data );
        { 
            string inf;
            getline( ss, inf );
            stringstream( inf ) >> sSwordLvlR;
            cout<<" "<<sSwordLvlR;  
        }
    }
    system("Pause");
    return 0;
}
/* I have not be able to figure out how to make it take the data for the Names yet either */

Example.csv

sSwordName,Wooden Shortsword,Bronze Shortsword,Iron Shortsword,Steel Shortsword,Titanium Shortsword
sSwordLvlR,1,3,5,6,10
sSwordMinAtk,0,0,0,0,0
sSwordMaxAtk,4,6,10,14,20
sSwordAtkRate,0,2,3,4,6
sSwordP,10,20,40,60,100
sSwordV,5,10,18,25,50
sSwordStrR,1,6,11,16,22
sSwordAtkR,1,6,11,16,22
sSwordIntR,1,3,5,8,12

Example1.csv

sSwordName,lvlReq,minAtk,maxAtk,atkRate,sPrice,sValue,strReq,atkReq,intReq
Wooden Shortsword,1,0,4,0,10,5,1,1,1
Bronze Shortsword,3,0,6,2,20,10,6,6,3
Iron Shortsword,5,0,10,3,40,18,11,11,5
Steel Shortsword,6,0,14,4,60,25,16,16,8
Titanium Shortsword,10,0,20,6,100,50,22,22,12

正如我在cpp中所说,我真的非常感谢任何可以帮助我解决该代码的人,这是理想的工作状态......以及任何想要提出改进思路以改善整体流程的人...... / p>

谢谢大家, Leaum

外部链接:

Example.cpp - http://pastebin.com/URWTGVq6 Example.csv - http://pastebin.com/924wvVX2

1 个答案:

答案 0 :(得分:1)

你需要的是一个函数,给定一个包含逗号分隔值的字符串将返回这些值的向量(作为字符串)。然后,当你有这个向量时,你可以进行你需要的其他处理(忽略第一列,将字符串转换为整数等)

这是该功能。

#include <vector>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

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;
}

int main()
{
  vector<string> v = split_at_commas("broad sword,abc,123");
  cout << v[0] << '\n' << v[1] << '\n' << v[2] << '\n';
}

输出:

广宝剑 ABC
123

当你遇到这样一个棘手的问题时,尝试将其分解成更小的部分是有帮助的。看看你的问题很明显,你需要一个函数来用逗号分割字符串,所以忘记问题的其余部分并首先编写该函数。然后在编写和工作时,将其应用于问题的其余部分。

修改

这里举例说明如何使用上述函数为sSwordLvlR数组赋值

// read the next line from csv file
vector<string> values = split_at_comma(line);
if (values[0] == "sSwordLvlR") // are we at the sSwordLvlR row?
{
  for (int i = 1; i < values.size(); ++i) // starting at the second column
  {
    sSwordLvlR[i - 1] = convert_string_to_integer(vector[i]);
  }
}

convert_string_to_integer是你应该编写的另一个函数,另一个例子是你应该如何通过编写更小的函数来分解复杂的探测器,然后你可以用它来解决更大的问题。

修改

要将此功能用于整个CSV文件,您必须一次读取一行文件并将split_at_commas功能应用于每一行。喜欢这个

string row;
while (getline(wInv, row))
{
  vector<string> values = split_at_commas(row);
  // do something depending on the values you've got
}

希望这有帮助。