开发者

Visual Studio C++ Inputting Data from CSV File into Variable Arrays

I have been trying to figure this little section of my larger project out for a while now. It has stumped me pretty well too... I am trying to take information from an Excel CSV File (See Example.CSV) and have it input into the Cpp project.. Now before I get to the way I have been working on it, I thought I might first run some other ways by ya guys and see which you thought would be most efficient?

1: Example1.csv: Item Values still following the; sSwordName, lvlReq, minAtk, maxAtk, atkRate, sPrice, sValue, strReq, atkReq, intReq. Column Headers as shown in Example.csv.. Would it be ideal to make like a header that pulls Item Information from the CSV file when told to, For example: If the player is at a shop, he wants to appraise his sword. When he clicks on the value button, it searches the 'Sword.csv' file for the item ID(Maybe) and will return the '5th', given the value of the 5th column in that items row is the Value it's worth.. And just create some kind of function that does that for everything? If so, any ideas how to do that.

2: The main way I have been trying to get this to work: in Example.csv I have all the items values I use in my program.. They are assigned to int and char variables, at least that's my goal.. Please direct to Example.cpp for my (fail) code and explanation..

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

As I said in the cpp, I would really really really appreciate anyone who can help me work out that code to it's ideal working state... As well as anyone who wishes to contribute ideas to improve it's overall flow..

Thanks everyone, Leaum

External Links:

Example.cpp - http://pastebin.com/URWTG开发者_运维技巧Vq6 Example.csv - http://pastebin.com/924wvVX2


What you need is a function which given a string containing comma separated values will return a vector of those values (as strings). Then when you have that vector you can do the other processing you require (ignoring the first column, converting the strings to integers etc)

Here is that function.

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

Output:

broad sword
abc
123

When you have a difficult problem like this it helps to try and break it down into smaller pieces. Looking at your problem it's obvious that you need a function to split strings at commas, so forget about the rest of the problem and write that function first. Then when it's written and working, apply it to the rest of the problem.

EDIT

Here for example is how you would use to the above function to assign values to your sSwordLvlR array

// 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 is another function you should write, and another example of how you should break down a complex probem by writing smaller functions, which you can then put toegther to solve the bigger problem.

EDIT

To use this function for the whole CSV file, you have to read your file one line at a time and apply the split_at_commas function to each line. Like this

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

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜