Чтение определенных целых чисел из файла с символами в вектор (C ++)

Я использовал функцию поиска, но не смог найти то, что искал.

У меня есть следующая проблема:

Есть такой файл:

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

Я хочу прочитать 1-е, 5-е и 6-е целые числа в трехмерном векторе.
после этого я хочу отсортировать данные и т.д .. но я просто не могу прочитать это в

Вот что у меня есть:

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

-4

Решение

Цикл ввода должен быть while (file >> tmpNumPoints >> tmpint >>
tmpint >> tmpint >> tmpNumFired >> tmpNumGot). Понятия не имею, кто ты
делать с getline; или временная строка, так как нет слов в
между вашими номерами … если у вас все еще есть проблемы, то напишите
MCVE, который выводит что-то и объясняет, чем вывод отличается от
что ты ожидал — М.М

Привет, я прошу прощения за этот плохой пост. Благодаря М.М у меня теперь правильный вывод.

я хотел это:
https://suckmypic.net/84503/aim.png

вот исправленный код:

#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();
}

Спасибо всем и еще раз — прошу прощения за мой плохой стиль

1

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector