How to fill a 2D array with numbers from file ?

Hey guys, I am new to the c++ world. Since today i've done all my work with simple shell scripting using gawk and sed. But the work i have to do takes way to long, so i want to learn C++.

And now I am sitting in front of my first problem and i cant find an answer to solve the problem.

I have to work with files in the form:

0100000001000000001100000000
0000000001000000001100000000
0001000100101000000000010001
0001000000100000000000010001
1111000010100111100000010001...

only much longer, up to 900mb per file.
But first for a unknown length per line and a given number of lines.
For the first steps i made small files with 100 lines and a line-length up to 200.

I wanted to safe these data in a 2D array, than add up the content of every column and safe it to a new array.

so in this example it would be:
1213000112301111102200030003

But theres my mainproblem for the moment, if i read in the data and try to safe it to an 2D array, the program reads the whole line and puts it into one point of the array (f.e.: arr[1][1]= 0100000001000000001100000000) instead of put every char of the line in one separated point of the array like:

arr[1][1]=0
arr[1][2]=1
arr[1][3]=0
arr[1][4]=0

I've written:

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
32
33
34
35
36
37
38
39
40
41

int samplesize = 100;					// should be given by user, only for testing
int samplelength = 200; 				// should be detected automatic, only for testing

int main ()
{
int numlines, numlines_beta = 0;


ifstream tin("testfile_small.msms");

while ( tin.good() ){
   std::string line;
   std::getline(tin, line);
   ++numlines_beta;
}

numlines = (numlines_beta-1);
cout << numlines << endl;

string paket_array[numlines][500];			



int plus1 = 0;

ifstream band("testfile_small.msms");

while ( band.good() ){
	for (int baat = 0; baat <= numlines-2; baat++) {
		std::string line;
		std::getline(band,paket_array[baat][plus1], '\n');
		plus1++;
	}
	
	cout << paket_array[300][plus1-50] << endl;
	
}									

return 0;
}


This code may contain useless stuff, but i cut it out of a quite bigger content of fail-code.

Maybe i am just to stupid, but i thought that every single char would get stored in a separate place and not a whole line.

Hope u can help me.

PS: sorry for my bad english :D

I hope it won't kill u !
Last edited on
i could just type an example but i doubt id be very much helpful.
go here and search for array.. a very useful example is there

http://www.youtube.com/thecplusplusguy
do you want to read all of the 900mb in one go? thats places the complete content of the file in RAM. Thats not a real good idea i think, but if you want that it is possible...
Your problem is that you are using the getline() function instead of get() in your 2nd reading of the file, so you're reading an entire line into your string. Try 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <ctype.h> //for checking whitespace

int main ()
{
int numLines = 200;  //you said this was given?
int lineLength = 0;

ifstream tin("testfile_small.msms");

while ( tin.good() )
{
     char value;
     tin.get(value);

     if (int(value).isspace == 0)
     {
         lineLength++;    
     }
     else
     {
         break;
     }
}

char paket_array[lineLength][numLines]; //You had these dimensions backwards, and you want a char array, not string 

ifstream band("testfile_small.msms");

while ( band.good() )
{
     for (int Y = 0; Y < numLines; Y++) 
     {
          for (int X = 0; X < lineLength; X++)		
          {
              char value;
	      band.get(value);
              
              if (int(value).isspace != 0) 
              {
                  break;  //we have reached the end of the line
              }
             
              paket_array[X][Y] = value;
          }
     }
}									

return 0;
}


This code may not be fully functional as I don't have more time to work on it but it should work.
Last edited on
Topic archived. No new replies allowed.