transfer data from string to array

hello, I have string or data in text file like this:
001100101 101100111 (3 spaces in the middle)
I need to transfer these data to two arrays (matrix) like this:
001
100
101
and
101
100
111
if someone can helps me, I'll be grateful
Last edited on
stringstreams will take care of the spaces for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
string original = "001100101   101100111";
stringstream iss;
char array1[3][3], array2[3][3];

iss << original;

for (int i = 0; i < 3; i++)
  for (int j = 0; j < 3; j++)
    iss >> array1[i][j];

for (int i = 0; i < 3; i++)
  for (int j = 0; j < 3; j++)
    iss >> array2[i][j];


Edit:
Wait, I didn't read that this stuff was in a text file. It's already in a stream, you don't need stringstream for it then. Just do it this way:

1
2
3
4
5
6
7
8
9
10
ifstream fin("input.txt");
char array1[3][3], array2[3][3];

for (int i = 0; i < 3; i++)
  for (int j = 0; j < 3; j++)
    fin >> array1[i][j];

for (int i = 0; i < 3; i++)
  for (int j = 0; j < 3; j++)
    fin >> array2[i][j];

[/code]
Last edited on
thanks a lot, but if I have longest data for example
001100101 101100111 111100000 .... and I need to transfer them to many arrays what will I do?
If you don't want to use vectors, you could add a third dimension to the array:

1
2
3
4
5
6
7
8
9
10
11
ifstream fin("input.txt");
char array1[100][3][3];

int k;
for (k = 0; fin.good(); k++)
{
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
		fin >> array1[k][i][j];
	k++;
}


This will handle up to 100 of these patterns. k will tell you how many elements are actually full of real data.
thanks a lot again
For outputting data of Arrays I have added some code to your code:

1
2
3
4
5
6
7
8
9
for(int h=0; h<k; h++)
{
for (int i = 0; i < 3; i++)
{
  for (int j = 0; j < 3; j++)
	  cout<<array1[k][i][j];
  cout<<endl;
}
}


but it doesn't work and outputs Arrays of unknown symbols. What the problem might be? Is my code wrong? And could you tell me what fin.good() means, please.Or send me link of its description. I haven't found good description of it
Last edited on
replace line 6:
cout << array1[k][i][j];
with
cout << array1[h][i][j];

As for your question about .good() check this out:
http://cplusplus.com/reference/iostream/ios/good/

It will return false if the end of the file has been reached, or a bad type of data was read, or something fails. That should represent the end of you reading stuff.
thanks again
I have replaced
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
int main () 
{
ifstream fin("input.txt");
char array1[100][3][3];

int k;
for (k = 0; fin.good(); k++)
{
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
		fin >> array1[k][i][j];
	k++;
}
cout<<k<<endl<<endl;

for(int h=0; h<k; h++)
{
for (int i = 0; i < 3; i++)
{
  for (int j = 0; j < 3; j++)
	  cout<<array1[h][i][j];
  cout<<endl;
}
cout<<endl;
}


I have data
000000111 010101011 001101001 010101010
this code outputs that k=10
and 6 arrays of unknown symbols, 3 of them between real arrays and 3 at the and
what problem might be?
Last edited on
hello everyone! Can anyone answer me the last question
You increase k too much.

1
2
3
4
5
6
7
for (k = 0; fin.good(); k++)
{
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
		fin >> array1[k][i][j];
	k++;  //Here
}


How can I resolve this problem?
I have cut k , it seemed to be solved but when I print arrays the last array is printed with unknown symbols. k is one more than it is really
What problem might be?
Last edited on
Topic archived. No new replies allowed.