Thread: Reading from file into 2D vectors

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    7

    Reading from file into 2D vectors

    I am trying to read input from a text file into a 2-D vector where each of the rows should represent a vector itself. The first entry from each row should tell how many columns are in the row, then populate the column vectors, and the row vector.

    However, I am getting some errors in my output.

    In code form, I have tried to do this as

    Code:
    #include<iostream>
    #include<fstream>
    #include<vector>
    
    
    #define Numrow 10 // clinical data sets
    
    
    typedef std::vector<double> vec;
    typedef std::vector<vec> vec2;
    
    
    int main()
    {
        std::ifstream infile1("data1.txt");
    
    
    
    
    
    
        vec2 Timerow(Numrow);
    
    
        int n; // number of timepoints
    
    
       if (infile1.is_open())
        {
            for (int i = 0; i < Numrow; ++i)
            {
                vec tdata;
                infile1 >> n;
                double t0;
    
    
    
    
                for (int j = 0; j < n; ++j)
                {
                  infile1 >> t0;
                  tdata.push_back(t0);
                }
    
    
                Timerow.push_back(tdata);
    
    
            }
    
    
        for (vec tdata : Timerow) std::cout << tdata.size() << std::endl;
    
    
        }
    
    
        else
        {
         std::cerr << "The file is not open" << std::endl;
        }
    
    }
    Data.txt
    2 0.1 0.2
    3 0.2 0.3 0.4
    4 0.3 0.4 0.5 0.6
    5 0.4 0.5 0.6 0.7 0.8
    6 0.5 0.6 0.7 0.8 0.9 0.10
    2 0.1 0.2
    3 0.2 0.3 0.4
    4 0.3 0.4 0.5 0.6
    5 0.4 0.5 0.6 0.7 0.8
    6 0.5 0.6 0.7 0.8 0.9 0.10
    When I try to test the number of columns on each row I get this output:

    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    2
    3
    4
    .
    .
    .
    6
    why am I getting the first 10 zeros? Is there something I am doing wrong? If so, where and how can I fix it?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,635
    You initialized Timerow to already have 10 elements (10 empty vectors). Then you read a line of data into a new vector and then PUSH THAT NEW VECTOR to Timerow, adding it as the 11th element, etc.

    You can fix it by not initializing Timerow with empty vectors.
    Code:
        //vec2 Timerow(Numrow);    // not this
        vec2 Timerow;              // THIS
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    7
    Ha! Thanks a bunch. I don't know how I missed that but I am extremely grateful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use vectors to read a file and then print info.
    By hamzams25 in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2016, 11:35 PM
  2. Load Strings into Vectors From a File
    By Ralek in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2014, 11:29 PM
  3. How to input a data file to parallel vectors
    By Wink- in forum C++ Programming
    Replies: 13
    Last Post: 10-30-2007, 05:24 PM
  4. Problems loading a text file into a struct of vectors
    By alt234 in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 09:58 PM
  5. Reading in vectors
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 03:28 PM

Tags for this Thread