I have a file that has the layout:

0 0
0 1
1 0
1 1

I want to read it into a multidimensional array and use the values. The thing is I don't know how to read it in.. I can read the amount of columns and rows the file has.. but I don't know how to read the values.

Also I want it to make a Multidimensional array depending on the amount of columns and rows and then read them into their respective cells.. If the file only contains numbers.. How do I make it read the values in as Integers?

How do I do it?

My Attempt.. It's not homework but it's nice to know how..

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <sstream>

using namespace std;

int main()
{
    int col, row;
    row = 0;
    col = 0;

    fstream InFile;
    InFile.open("test.txt", ios::in | ios::out);
    string line;

    while(getline(InFile, line))
    {
        row++;
        stringstream s(line);
        string token;
        while(getline(s, token, ' '))
        {
            col++;
            table[row][col];
        }
    }

         //table [row][col];
    string table [row][col];        //Create A MultiDimensional Array depending on the amount of columns and rows..

    cout <<"Rows: "<< row << endl;
    cout<<"Columns: "<< col/row <<endl;
    cout<<table[1][2];
    InFile.close();
}

Recommended Answers

All 3 Replies

What does your code do now?

Does it compile? Are any errors created when you run the program?

I have posted three small sample programs off the following page:

Dynamic Arrays in C++ - Some Basic Examples

They use dynamic arrays and take in numbers of type double.

Hope this helps.

What does your code do now?

Does it compile? Are any errors created when you run the program?

I have posted three small sample programs off the following page:

Dynamic Arrays in C++ - Some Basic Examples

They use dynamic arrays and take in numbers of type double.

Hope this helps.

My code compiles.. No errors. It tells you how many rows and columns are found in the file.. I just don't know how to grab the values and put them in the array is all.

Can someone please help me.. I have just figured out how to read all the values into a multidimensional array.. the thing is the table in the file can change at any time.. It can be any size.. Example:

Old Table to read (not a uniformed table, but all values print):

0 0
0 1
1 0
1 1

New Table to read (does not print all values.. not in order either):

0 0 0 0
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 0
1 1 1 1

Deformed Table:

0 6 12
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17 18           //<-- See the deformed 18? table is not in uniformed rows and columns either.. so the last value never prints.

See how the dimensions of the table changed from 4x2 to 10x4? I want my program to adapt to such changes and be able to give the location of any value. Help please! I've been trying for so long.

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{

    int col, row, x, y;
    x = y = 0;
    row = 0;
    col = 0;

    fstream File;
    File.open("test.txt", ios::in | ios::out);
    string line;

    while(getline(File, line))
    {
        x++;
        istringstream tokenizer(line);
        string token;

        while(getline(tokenizer, token, ' '))
        {
            y++;
        }
    }

    row = x;
    col = y/x;

    x = y = 0;      //Reset X and Y..
    File.clear();   //Reset File Reading Stream..
    File.seekg(ios_base::beg); //Set Reading Position to the beginning of the file..

    float table [row][col];        //Create A MultiDimensional Array depending on the amount of columns and rows..

    for(x = 0; x < row; x++)
    {
        if(getline(File, line))
        {
            istringstream tokenizer(line);
            string token;

            for(y = 0; y < col; y++)
            {
                if(getline(tokenizer, token, ' '))
                {
                    istringstream float_iss(token);
                    float_iss >> table[x][y];
                    cout<<table[x][y]<<endl;
                }
            }
        }
    }
    File.close();
    cout<<"Rows: "<<x<<endl;
    cout<<"Cols: "<<y<<endl;
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.