-4

I used the search function but could not really find what i was searching for.

I have the following problem:

There is a file like this:

370 18 28 1 129 120 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 
370 18 28 1 129 78 
0 0 0 0 0 0 

https://suckmypic.net/84499/data.png

I want to read the 1st, 5th and 6th integers into a 3-dimensional vector. after that i want to sort the data etc.. but i just cant read this in

Here is what i have:

#include <fstream>
#include <iostream>

int main{

    //first open the file
    std::fstream file;
    file.open("highscore.txt", std::ios::in);
    if (file.fail()){
        perror("could not load highscore.txt file\n");
    }

    //create some stuff for temp save
    std::string tmp;
    int tmpNumPoints, tmpNumFired, tmpNumGot, tmpint;

    //now extract the integer we need (points, shots fired, shots got)
    while (std::getline(file, tmp, '\n')){
        //IN-(1)STRING--POINTS--------(2)--------------(3)---------------(4)--------------(5)----Shots fired----(6)----Shots got
        file >> tmpNumPoints >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpNumFired >> tmp >> tmpNumGot >> tmp;

        //and store it into the integer
        //------------------------X--------------Y----------Z-------
        m_numInput.emplace_back(tmpNumPoints, tmpNumFired, tmpNumGot);
    }

    //close the file
    file.close();

    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Jack
  • 11
  • 4
  • 1
    What are you trying to do? Were are you getting stuck? What's not working? What is working? What are you expecting. So many questions, so few answers. – Captain Obvlious Sep 09 '15 at 21:09
  • sorry. what i am trying: i want a vector3i (3 dimensional - type integer) as the following: https://suckmypic.net/84501/aim.png – Jack Sep 09 '15 at 21:15
  • 1
    the file size is not static by the way :D – Jack Sep 09 '15 at 21:15
  • 2
    Hello and welcome to stackoverflow! Could you provide an example code that actually compiles, what we call an [MCVE](http://stackoverflow.com/help/mcve)? Like providing an `int main()` with parenthesis, and also where does `m_numInput` comes from and what it is ? Also what is your _actual_ issue? Are you getting an error you cannot understand? Is there a bug in your program you don't understand? Try asking a very specific question, and make a single question for each issue you're trying to solve. The more focused your questions are, the better the answers will be! – zmo Sep 09 '15 at 21:15
  • 1
    sorry for being so unspecific. i dont know how to edit my post. it looks terrible. if i find it i will rework it – Jack Sep 09 '15 at 21:18
  • The input loop should be `while(file >> tmpNumPoints >> tmpint >> tmpint >> tmpint >> tmpNumFired >> tmpNumGot)` . No idea what you are doing with `getline`; or the temp string as there are no words in between your numbers... if you are still having trouble then post a MCVE which outputs something and explain how the output differs from what you expected – M.M Sep 09 '15 at 21:18
  • @user2917285, hint: click the "edit" button below the post – zmo Sep 09 '15 at 21:19
  • When you say you want a three dimensional array, do you mean an array with three elements (one each for x, y, and z for example) or an array shaped like a Rubic's cube? – user4581301 Sep 09 '15 at 21:23

1 Answers1

1

The input loop should be while(file >> tmpNumPoints >> tmpint >> tmpint >> tmpint >> tmpNumFired >> tmpNumGot) . No idea what you are doing with getline; or the temp string as there are no words in between your numbers... if you are still having trouble then post a MCVE which outputs something and explain how the output differs from what you expected – M.M

Hi, i am sorry for this bad post. Thanks to M.M i now have the correct output.

i wanted this: https://suckmypic.net/84503/aim.png

here is the corrected code:

#include <fstream>
#include <iostream>
#include <vector>

int main(){

    std::vector<Vector3i> m_numInput; ///< this is a vector of a sfml specific vector(x,y,z type int)

    //first open the file
    std::fstream file;
    file.open("highscore.txt", std::ios::in);
    if (file.fail()){
        perror("could not load highscore.txt file\n");
    }

    //create some stuff for temp save
    int tmpNumPoints, tmpNumFired, tmpNumGot, tmpint;

    //now extract the integer we need (points, shots fired, shots got)
    while (file >> tmpNumPoints >> tmpint >> tmpint >> tmpint >> tmpNumFired >> tmpNumGot){

        std::cout << "X: " << tmpNumPoints << " Y: " << tmpNumFired << " Z: " << tmpNumGot << "\n";
        //and store it into the integer
        //------------------------X--------------Y----------Z-------
        m_numInput.emplace_back(tmpNumPoints, tmpNumFired, tmpNumGot);
    }

    //close file
    file.close();
}

Thank you all, and again - please excuse my bad style

Jack
  • 11
  • 4