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
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];
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
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.
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?
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?