Issue reading info from file.

Hey all. Writing a little program to help me with EVE online. SPREADSHEETS, WOO. Anyway, having a problem reading several rows of data in from a file. My prog will read in the first line and correctly fill the first class in an array with it, but after that it doesn't seem to be reading anything else in. Here's my code to read in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Ore OreArr[48];
    string tempOreIn;
    string OreID;
    string tempName;
    float tempVol;
    float tempCompVol;
    int tempTrit;
    int tempPye;
    int tempMex;
    int tempIso;
    int tempNoc;
    int tempMeg;
    int tempZyd;
    int tempMor;
    std::stringstream strcon;

    for(int i = 0; i < 48; i++)
    {
        OreArr[i].SetMins(0,0,0,0,0,0,0,0);
        OreArr[i].SetVol(0,0);
        OreArr[i].SetSaleVal(0,0,0);
    }

while (getline(ore_in, tempOreIn, ';'))
    {
        strcon << tempOreIn;
        strcon >> OreID >> tempName >> tempVol >> tempCompVol >> tempTrit >> tempPye >> tempMex >> tempIso >> tempNoc >> tempMeg >> tempZyd >> tempMor;

        OreArr[GetOreEle(OreID)].SetName(tempName);
        OreArr[GetOreEle(OreID)].SetVol(tempVol, tempCompVol);
        OreArr[GetOreEle(OreID)].SetMins(tempTrit, tempPye, tempMex, tempIso, tempNoc, tempMeg, tempZyd, tempMor);
    }


Basically, I am pulling an entire line from the file and dropping it into a string, then moving it from the string into a stringsteam. Then I extract the data into a couple temp variables to store. Then I use the OreID to get the element corresponding to the Ore object I want to manipulate in the Ore array (using a function I made which is basically a bunch of if's.)

Now the first line reads in fine. After that though nothing else seems to be read. At least there's nothing in the classes when I output them. So, either the rest of the lines after the first aren't being read into the temp variables (IE it's a problem with my while block, or something is futzy with my output code (which I think is actually solid...)

Code is on pastebin http://pastebin.com/AukS056A

I know it's not the prettiest thing ever, atm I'm more worried about getting it working then being elegant with my coding. :p
Last edited on
Try moving the declaration of the std::stringstream inside the loop. Pass tempOreIn to the constructor, instead of using the operator<<() overload.
After that though nothing else seems to be read.

Look at this snippet:

1
2
3
4
5
6
7
8
9
10
11
   std::stringstream strcon;
...
while (getline(ore_in, tempOreIn, ';'))
    {
        strcon << tempOreIn;
        strcon >> OreID >> tempName >> tempVol >> tempCompVol >> tempTrit >> tempPye >> tempMex >> tempIso >> tempNoc >> tempMeg >> tempZyd >> tempMor;

        OreArr[GetOreEle(OreID)].SetName(tempName);
        OreArr[GetOreEle(OreID)].SetVol(tempVol, tempCompVol);
        OreArr[GetOreEle(OreID)].SetMins(tempTrit, tempPye, tempMex, tempIso, tempNoc, tempMeg, tempZyd, tempMor);
    }

Where in that snippet do you associate the string tempOreln to the stringstream?

@helios I think I already tried that and it failed worse then what it does now. I'm not 100% on that though so I will definitely give that another try.

@jlb strcon << tempOreIn; should read into the stringstream from the string tempOreIn shouldn't it? It works at least once since my first Ore object outputs ok.

Edit: Helios, did you mean something like this?

1
2
3
4
5
6
7
8
9
while (getline(ore_in, tempOreIn, ';'))
    {
        std::stringstream strcon(tempOreIn);
        strcon >> OreID >> tempName >> tempVol >> tempCompVol >> tempTrit >> tempPye >> tempMex >> tempIso >> tempNoc >> tempMeg >> tempZyd >> tempMor;

        OreArr[GetOreEle(OreID)].SetName(tempName);
        OreArr[GetOreEle(OreID)].SetVol(tempVol, tempCompVol);
        OreArr[GetOreEle(OreID)].SetMins(tempTrit, tempPye, tempMex, tempIso, tempNoc, tempMeg, tempZyd, tempMor);
    }


Won't that try to create multiple instances of strcon? Like, one for every time the loop occurs?
Last edited on
Won't that try to create multiple instances of strcon? Like, one for every time the loop occurs?
Sure, but:
1. File IO will utterly dominate the run time.
2. What you're doing is only marginally faster than that, anyway.

Ultimately, there's no point in worrying about the performance if the code doesn't work.
Last edited on
Can't argue with that logic. :)

Edit: That didn't work at all, doesn't even read in the first line of my file now and gets to the end of the GetOreEle() function to output the error message.
Last edited on
Post the text file.
Here it is. Basically structured as OreID, Ore Name, 2 floats representing volumes, and then 8 ints representing mineral amounts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Vel Veldspar 0.1 0.15 415 8 7 6 5 4 3 2;
CVel Concentrated Veldspar 0.1 0.15 436 0 0 0 0 0 0 0;
DVel Dense Veldspar 0.1 0.15 457 0 0 0 0 0 0 0;
Sco Scordite 0.15 0.19 346 173 0 0 0 0 0 0;
CSco Condensed Scordite 0.15 0.19 363 182 0 0 0 0 0 0;
MSco Massive Scordite 0.15 0.19 380 190 0 0 0 0 0 0;
Pyr Pyroxeres 0.3 0.16 351 25 50 0 5 0 0 0;
SPyr Solid Pyroxeres 0.3 0.16 368 26 53 0 5 0 0 0;
VPyr Viscous Pyroxeres 0.3 0.16 385 27 55 0 5 0 0 0;
Pla Plagioclase 0.35 0.15 107 213 107 0 0 0 0 0;
APla Azure Plagioclase 0.35 0.15 112 224 112 0 0 0 0 0;
RPla Rich Plagioclase 0.35 0.15 117 234 117 0 0 0 0 0;
Omb Omber 0.6 0.07 85 34 0 85 0 0 0 0;
SOmb Silver Omber 0.6 0.07 89 36 0 89 0 0 0 0;
GOmb Golden Omber 0.6 0.07 94 38 0 94 0 0 0 0;
Ker Kernite 1.2 0.19 134 0 267 134 0 0 0 0;
LKer Luminous Kernite 1.2 0.19 140 0 281 140 0 0 0 0;
FKer Fiery Kernite 1.2 0.19 147 0 294 147 0 0 0 0;
Jas Jaspet 2 0.15 72 121 144 0 72 0 3 0;
PuJas Pure Jaspet 2 0.15 76 127 151 0 76 0 3 0;
PrJas Pristine Jaspet 2 0.15 79 133 158 0 79 0 3 0;
Hem Hemorphite 3 0.16 180 72 17 59 118 0 8 0;
VHem Vivid Hemorphite 3 0.16 189 76 18 62 123 0 9 0;
RHem Radiant Hemorphite 3 0.16 198 79 19 65 129 0 9 0;
Hed Hedbergite 3 0.14 0 81 0 196 98 0 9 0;
VHed Vitric Hedbergite 3 0.14 0 85 0 206 103 0 10 0;
GHed Glazed Hedbergite 3 0.14 0 89 0 216 108 0 10 0;
GneGneiss 5 1.03 1278 0 1278 242 0 0 60 0;
IGne Iridescent Gneiss 5 1.03 1342 0 1342 254 0 0 63 0;
PGne Prismatic Gneiss 5 1.03 1406 0 1406 266 0 0 65 0;
DOch Dark Ocher 8 3.27 8804 0 0 0 173 0 87 0;
OnOch Onyx Ocher 8 3.27 9245 0 0 0 182 0 91 0; 
ObOch Obsidian Ocher 8 3.27 9685 0 0 0 190 0 95 0;
Spo Spodumain 16 16 39221 4972 0 0 0 78 0 0;
BSpo Brige Spodumain 16 16 41182 5221 0 0 0 82 0 0;
GSpo Gleaming Spodumain 16 16 43143 5496 0 0 0 86 0 0;
Cro Crokite 16 7.81 20992 0 0 0 275 0 367 0;
SCro Sharp Crokite 16 7.81 22041 0 0 0 290 0 385 0;
CCro Cystalline Crokite 16 7.81 23091 0 0 0 309 0 403 0;
Bis Bistot 16 6.11 0 16572 0 0 0 118 236 0;
TBis Triclinic Bistot 16 6.11 0 17402 0 0 0 124 248 0;
MBis Monoclinic Bistot 16 6.11 0 18230 0 0 0 130 259 0;
Ark Arkonor 16 3.08 6905 0 1278 0 0 230 115 0;
CArk Crimson Arkonor 16 3.08 7251 0 1342 0 0 242 121 0;
PArk Prime Arkonor 16 3.08 7596 0 1406 0 0 253 127 0;
Mer Mercoxit 40 0.1 0 0 0 0 0 0 0 293;
MMer Magma Mercoxit 40 0.1 0 0 0 0 0 0 0 308;
VMer Vitreous Mercoxit 40 0.1 0 0 0 0 0 0 0 323;


When I cout all the Ore classes data the first one (Veldspar) looks good with all the proper values, the rest show either 0 (for numerical data types) or "None" for the name.
Last edited on
You're going to have problems, you're trying to input string string float, which is fine for the first line but most of the rest of the lines have string string string float which will cause the stream to error when you try to extract the numeric entry. Remember the extraction operator stops processing strings when it encounters a white space character.



Yeah, this is badly formatted. Try to get it formatted such that each column is separated by the same character. Tab ('\9') and comma are common choices.
I did try commas but then ran into the issue of how to denote end of line, which is why I went to spaces and just reading a whole line into a string, then dumping that into a string stream and extracting the data from that.

And ok, I didn't even think of the names that have spaces in them... Hmmm. Ok, back to using commas or something else, then I can use getline with the comma as a delimiter, but how to denote end of line so I can start at the beginning of the while loop. Gonna have to rethink this I guess. :) Thanks for the help hehe.
I did try commas but then ran into the issue of how to denote end of line, which is why I went to spaces and just reading a whole line into a string, then dumping that into a string stream and extracting the data from that.

When using comma separated values normally the last "field" doesn't have a comma, instead there is the end of line character.

but how to denote end of line so I can start at the beginning of the while loop.

You should still use getline() to extract the entire line into a string then use an istringstream to process that string. By defalut getline() retrieves a "complete line", hence it's name.
Last edited on
Ah, so leave the semicolon off the end and getline will still work, without even need to pass a delimiter to it?
Yep.
Only problem is, you can't use operator>>() anymore. You need to tokenize the string manually.
Guess I'ma have to learn how to do that then. :) Thanks for the help guys!
Remember when parsing the line you can use getline() with the optional delimiter to retrieve strings, then use the extraction operator to extract the numbers, but you must also extract the delimiter. For example:
1
2
3
4
5
6
7
8
9
char delimiter;
istringstream ins;
...
// You have already read a line of text into a variable named line.

   istringstream ins.str(line); // Associate the stream to the line.
   getline(ins, id);
   getline(ins, name);
   ins >> number1 >> delimiter >> number2 ... >> lastnumber;

Ya, that explains why my last attempt last night didn't work. Heh. My next thought was to use getline for each of the numerical values, then stof and stoi on the returned string, but I think using the >> is simpler so I'll give that a whorl. Thanks again, definitely a learning experience... and I thought I had input down pat. Must have forgotten too much. :p

Edit: *sigh* stof and stoi don't work with the newest version of GCC/MinGW supplied with code::blocks... think I might swap back to MSVC++
Last edited on
Topic archived. No new replies allowed.