Visual Studio C++ Eingabe von Daten aus CSV-Datei in der Variablen Arrays

Habe ich versucht, das Bild dieser kleine Teil meines größeren Projekts, das heraus für eine Weile jetzt. Es verblüfft mich ziemlich gut zu... ich bin versucht, die Informationen aus einer Excel-CSV-Datei (Siehe Beispiel.CSV) und haben es Eingangs in der Cpp-Projekt.. Nun, bevor ich auf die Weise habe ich daran gearbeitet, ich dachte, ich könnte vielleicht ersten Lauf einige andere Arten von ya Jungs und sehen, was Sie dachten, es wäre am effizientesten?

1: Beispiel1.csv: Item-Werte noch nach der; sSwordName, lvlReq, minAtk, maxAtk, atkRate, sPrice, sValue, strReq, atkReq, intReq. Spaltenüberschriften wie in Beispiel.csv.. Wäre es ideal, um wie ein header, der zieht Informationen zum Eintrag aus der CSV-Datei, wenn Sie gesagt hat, Zum Beispiel: Wenn der Spieler ist in einem shop, er will zu begutachten sein Schwert. Wenn er klickt auf die Schaltfläche Wert, Sie sucht das 'Schwert.csv' - Datei für die item-ID(Vielleicht) und zurück, '5', da der Wert der 5. Spalte, die Elemente der Zeile ist der Wert, den es lohnt sich.. Und nur eine Art von Funktion, die das tut für alles? Wenn ja, irgendwelche Ideen, wie das zu tun.

2: Der wichtigste Weg, ich habe versucht, diese zu arbeiten:
in Beispiel.csv habe ich alle items, die Werte, die ich verwenden in meinem Programm.. Sie zugewiesen sind, die int-und char-Variablen, zumindest ist das mein Ziel.. Bitte direkt an Example.cpp für mein (fehl) - code und Erklärung..

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 */

Beispiel.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

Beispiel1.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

Wie gesagt in der cpp, ich wäre wirklich wirklich wirklich dankbar, wer mir helfen kann, code, ist es ideal funktionierenden Staat... So gut wie jeder, der beitragen möchte, Ideen zur Verbesserung es Gesamt-flow..

Danke an alle,
Leaum

Externe Links:

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

Gerade gemerkt, vielleicht wäre es einfacher machen, wenn die Variablen entsprechen, um Weise in der CSV als auch in der cpp? Nicht sicher, nur so ein Gedanke.
Wieder, ich bin müde von dem Lesen weggeworfen code aus DB 🙁 echt traurig!!!!!
Sie können ändern Sie das format der csv, oder ist das behoben? Parsing-sowas wäre viel natürlicher, wenn Sie alle Werte pro Schwert auf einer Linie, anstatt die andere Weise herum, wie es jetzt ist.
Eigentlich kann ich es ändern, um einfach über alles, nehme ich an? Ich meine, die Grundidee wird in der Lage sein, um es zu öffnen in Excel, alles in spread sheets und dann änderungen oder Ergänzungen.. Es kann sein .txt-oder einfach nur über alles, was Excel tun, meine Frage ist, wäre es noch compat. mit excel? Auf einer anderen Anmerkung, ich kann sogar kompilieren Sie die .csv-so wie es sein würde,; {1,3,5,6,10} (sSwordLvlR), So würde es nur eine Feste Zelle. Ob das Sinn macht
Wie ist es nicht? Bekommt man keine Daten aus der Datei? Läuft das Programm? Tut es kompilieren?

InformationsquelleAutor Leaum | 2011-08-27

Schreibe einen Kommentar