1

Sorry, I am a bit new to c++, but I need to organize data from a txt file into an array (or vector if that's easier), and it needs to have 12 columns and 10000 rows. I need to be able to multiply these columns, but I can't get past putting data into rows. The data is parsed by tabs, and is already in a 12x10000 format. How can I do this only using c++?

I've already tried looking online and I've come with nothing other than to read the text. I have 225 more lines of code that are all the tries I've had at trying to do this. It essentially boils to these lines. I have a parser, but it doesn't do anything but divide the data by tabs, not identify it.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    float array[12][10000]; // creates array to hold names
    long loop=0; //short for loop for input
    float line; //this will contain the data read from the file
    ifstream myfile ("data.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
        while (! myfile.eof() ) //while the end of file is NOT reached
        {
            getline (myfile,line); //get one line from the file
            array[loop] = line;
            cout << array[loop] << endl; //and output it
            loop++;
        }
        myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output
    system("PAUSE");
    return 0;
}

I expected the outcome to be the data organized into an array or vector (I don't know how to use vectors), inwhich I can multiply columns, but it just falls with the error that I cannot properly put the code in the columns.

  • `while (! myfile.eof() )` https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Mar 30 '19 at 02:37
  • Multidimensional arrays in C++ don't work quite like you're trying to use them here, check: https://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm - with 2 dimensions you need to loop over two variables, like the example in the link does. – mock_blatt Mar 30 '19 at 02:41

2 Answers2

1

Here a simple solution that works for separators that are tabs or spaces.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

constexpr size_t rows_len = 10000;
constexpr size_t cols_len = 12;

int main ()
{
    float array[rows_len][cols_len]{}; // value initialization to ensure unfilled cells at 0
    ifstream myfile("data.txt");
    if (!myfile.is_open()) {
        cout << "Unable to open file" << endl;
        return 1;
    }
    string line;
    for (size_t row = 0; row < rows_len && myfile; row++) {
        getline(myfile, line);
        const char* s = line.c_str();
        for (size_t col = 0; col < cols_len; col++) {
            char* p = nullptr;
            array[row][col] = strtof(s, &p);
            s = p;
        }
    }

    // use array ...

    return 0;
}

Second argument of strtof() allows to know where is the beginning of the next cell. In case a cell is not a number, all the remaining row of array is set to 0.

Jérôme Migné
  • 219
  • 1
  • 5
0

If you are sure that the input format will be respected, you can simply let the iostream module decode the floating point values:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    float array[12][10000]; // creates array to hold names
    long loop=0; //short for loop for input
    float line; //this will contain the data read from the file
    ifstream myfile ("data.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
        for(int line=0; line <1000; line++) //read 1000 lines
        {
            for (int col=0; col<12; col++)  // 12 values per line
            {
                myfile >> arr[col][line];
                if (!myfile)
                {
                    cout << "Read error line " << line << " col " << col << "\n";
                    myfile.close();
                    return 1;
                }
            }
        }
        myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output
    system("PAUSE");
    return 0;
}

Please note that this code does not use the frowned upon while (! myfile.eof() ), but tests immediately after a read

Serge Ballesta
  • 121,548
  • 10
  • 94
  • 199