.txt into a 2d array

I am writing some code to read the contents of a very large .txt file (about 45 rows by 39000 columns) into a 2d array. Each number varies in size but they are each seperated by a tab.

The only major thing I have so far is a function to read the file and another function to make the 2d array in.

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
Table::Table(){

string line;
    
    ifstream myfile("data.txt" );
    
    if ( myfile.is_open ( ) ){
         
         while (!myfile.eof( )){
               
               getline(myfile,line);
         
         }//end while
         
         myfile.close ( ) ;
         int stop;
         cin >> stop;
         
    }//end if
    
    else cout << "Unable to open file";
    
} //end Table

void Table::make_array(){

}



How do I put a number from data.txt into an element of an array and then move on to the next element/number/column?

Any help would be greatly appriciated. I understand the logic I'm just not familiar with the code.
If you used char get() rather than string getline() inside a while loop you could write the characters up to the next space to an element of the array using a while condition of (!= " ").
It may be better to use a string vector rather than an array, loading with push_back () as you do not then need to identify it`s size (as you would with an array) prior to compile time.
Last edited on
Hope this will help!!
#include <conio.h>
#include <string>
using namespace std;

#define ROW 10
#define COLUMN 10

int main(int argc, char* argv[])
{
string str_name = "My name is bluecoder";
char name[10] ;
char* pch;
strcpy(name , str_name.c_str());

pch = strtok(name, "\t");

while (pch != NULL)
{


cout<<"\n"<<pch;
pch = strtok (NULL, "\t");
}
return 0;
}
Topic archived. No new replies allowed.