Attempting to get data from lines in a file

I'm quite new to C++, so sorry if this is a dumb question!

For a project we are given a file with a couple of thousand lines of values, each line having 9 different numbers.

I want to create a for/while loop that, for each loop, stores the 8th and 9th integer of a line as a variable so that I can do some calculations with them. The loop would then move onto the next line, store the 8th and 9th numbers of that line as the same variable, so that I can do the same calculation to it, ending when I've run out of lines.

My problem is less to do with reading the file, I'm just confused how I'd tell it to take only the 8th and 9th value from each line.

Thanks for any help, it is greatly appreciated!
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
#include <vector>
#include <string> //stoi : needs c++11
#include <fstream>
#include <sstream>
#include <iostream>
//uses ranged for loop : needs c++11
using namespace std;
int main()
{
    ifstream file;
    vector<vector<int>> numbers;
    string fileread;
    file.open("test.txt");
    while(getline(file, fileread))
    {
        stringstream rd(fileread);
        vector<int> read;
        if(!fileread.empty())
        while(getline(rd, fileread, ' ')) read.push_back(stoi(fileread));
        numbers.push_back(read);
    }
    for(auto a : numbers)
    {
        for(auto b : a) cout << b << "\t";
        cout << endl;
    }
}

stoi: string to int

getline's name could be "getsince", because it get all the contents since it separator, which is, by default, line-break ('\n').

Test this program with the file in http://pastebin.com/rUmWSRVM and save as "test.txt".

In your real program, to get the first number of the first line, use numbers[0][0]. The first of the second line numbers[1][0]. The second, from de second line numbers[1][1]...

Hope you appreciate this! :)
Last edited on
Thanks a lot!

I was just thinking, the text file I'm trying to input is about 100,000 lines long so maybe a vector isn't the best way to do this, would there be another way?
Im afraid no :/ Some genius can give you another reply. Just wait.
> I want to create a for/while loop that, for each loop, stores the 8th and 9th integer of a line
> as a variable so that I can do some calculations with them.
> The loop would then move onto the next line, store the 8th and 9th numbers of that line
> as the same variable, so that I can do the same calculation to it, ending when I've run out of lines.

Something like this:

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
#include <fstream>
#include <string>
#include <sstream>

int main()
{
     std::ifstream file( "file name here" ) ; // open the file

     const int N_DISCARDED = 7 ; // we are not interested in the first seven numbers

     std::string line ;
     while( std::getline( file, line ) ) // for each line in the file
     {
         // create an input string stream to read from the line
         std::istringstream stm(line) ;

         // extract and throw away the first NUMBERS_DISCARDED numbers
         double /* int? */ number ;
         for( int i = 0 ; i < N_DISCARDED ; ++i ) stm >> number ;

         // extract the 8th and 9th integers
         int number8 ;
         int number9 ;
         if( stm >> number8 >> number9 ) // if the line did contain nine numbers
         {
             // do some calculations with number8 and number9

             // store the result somewhere (if required)
         }
     }
}



> I was just thinking, the text file I'm trying to input is about 100,000 lines long
> so maybe a vector isn't the best way to do this,

On a typical implementation, 100K lines with two integers per line stored into a vector would take a maximum of about 1.6 MB. You can afford that quite easily, if storing all the numbers is actually needed.
Thanks a lot for the reply, some great things I can try implement and experiment with, been really helpful!
Topic archived. No new replies allowed.