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

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

一段时间以来,我一直在努力弄清楚我的大型项目的这一小部分。它也让我很困惑......我正在尝试从 Excel CSV 文件(参见 Example.CSV)中获取信息并将其输入到 Cpp 项目中......现在在我开始处理它之前,我想我可能会首先运行你们的一些其他方法,看看你们认为哪种方法最有效?

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

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

例子.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 */

例子.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

示例 1.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 中所说,我真的非常非常感谢任何可以帮助我将代码编写到理想工作状态的人......以及任何希望贡献想法以改善其整体流程的人......

谢谢大家 莲花

外部链接:

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

最佳答案

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

这是那个函数。

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

输出:

阔剑
美国广播公司
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
}

希望这对您有所帮助。

关于c++ - Visual Studio C++ 将 CSV 文件中的数据输入变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7213621/

相关文章:

visual-studio - 32位版本的Visual Studio Remote Debugging Monitor无法调试64位进程?

C++11 局部命名引用返回值(xvalue)?

C# 可能不是安全应用程序的最佳选择?

java - java将所有数组元素重新初始化为零

c++ - 使用 CMake 打开链接器标志

c++ - 为什么我不能给这个 int 赋值?

c++ - 如何修复未对齐的指针 (char**)

c++ - 这是什么意思 "!somePointer"?

javascript - 在JavaScript中,为什么不能修改常量String但可以修改常量数组

java - 为什么将数组传递给另一个方法会更改原始数组?