-
Help with streams
Why dose this not work:
Code:
char array[20][26];
ifstream arrayfile ( name );
if (arrayfile.fail())
{
cout << "Problems opening "<<name<< endl;
kill (1);
}
int xx = 0;
int yy = 0;
while ( xx != 26 )
{
while ( yy != 20 )
{
arrayfile>>array[yy][xx];
yy++;
}
xx++;
yy = 0;
}
output in array:
Code:
22222222222222222222
22222220000000000000
00000000000220000001
00000000000000000220
00011111011111111100
00022000011100000000
11110000022000011100
00000011110000022000
01110000000011110000
02200001110000001011
11000002200001110001
00001111000002200071
11000000001111000002
20000111000010001111
00004220000111000000
00111100000220000111
00000000111100000220
00011111111111111100
00022000011111111111
11110000022000111111
11111111110000022000
00000000000000000000
02200000000000000000
00000002200000000000
00000000000002222222
22222222222222222222
when the reverse works fine:
Code:
void array_to_file ( int array[20][26], char name[50] )
{
ofstream arrayfile ( name );
if (arrayfile.fail())
{
cout << "Problems opening "<<name<< endl;
kill (1);
}
int xx = 0;
int yy = 0;
while ( xx != 26 )
{
while ( yy != 20 )
{
arrayfile<<array[yy][xx];
yy++;
}
xx++;
arrayfile<<endl;
yy = 0;
}
arrayfile.close();
}
output in file:
Code:
22222222222222222222222222
20000000000000000000000002
20000001000000000000000002
20000111110111111111000002
20000111000000001111000002
20000111000000001111000002
20000111000000001111000002
20000111000000101111000002
20000111000100001111000002
20007111000000001111000002
20000111000010001111000042
20000111000000001111000002
20000111000000001111000002
20000111111111111111000002
20000111111111111111000002
20001111111111111111000002
20000000000000000000000002
20000000000000000000000002
20000000000000000000000002
22222222222222222222222222
How can it be done corectly?
the app is the attachment its a linux 32bit binery that depends on liballegro-4.9.12.so.
-
Your output adds newlines - arrayfile<<endl;
But your input does nothing to remove them.
-
thanks it works better now but i now it turns the array by 90 degrees? what gives:
Code:
void array_to_file ( int array[20][26], char name[50] )
{
ofstream arrayfile ( name );
if (arrayfile.fail())
{
cout << "Problems opening "<<name<< endl;
kill (1);
}
int xx = 0;
int yy = 0;
while ( xx != 26 )
{
while ( yy != 20 )
{
arrayfile<<array[yy][xx];
yy++;
}
xx++;
arrayfile<<endl;
yy = 0;
}
arrayfile.close();
}
void file_to_array ( char name[50] )
{
char buffer;
char array[20][26];
ifstream arrayfile ( name );
if (arrayfile.fail())
{
cout << "Problems opening "<<name<< endl;
kill (1);
}
int xx = 0;
int yy = 0;
while ( xx != 26 )
{
while ( yy != 20 )
{
arrayfile.get (buffer);
if (buffer != '\n' )
{
array[yy][xx] = buffer;
yy++;
}
}
xx++;
yy = 0;
}
arrayfile.close();
yy = 0;
xx = 0;
while ( xx != 26 )
{
while ( yy != 20 )
{
cout<<array[yy][xx];
yy++;
}
xx++;
cout<<endl;
yy = 0;
}
}
out put in file:
Code:
22222222222222222222
20000000000000000002
20000000000000000002
20000000000000000002
20000000070000010002
20011111111111110002
20011111111111110002
20111111111111110002
20010000000001110002
20010000000001110002
20000000000001110002
20010000100001110002
20010000001001110002
20010000000001110002
20010001000001110002
20010000000001110002
20011111111111110002
20011111111111110002
20011111111111110002
20011111111111110002
20000000000000000002
20000000000000000002
20000000000000000002
20000000000000000002
20000000004000000002
22222222222222222222
out put in array:
Code:
22222222222222222222
20000000000000000002
20000000000000000002
20000000000000000002
20000000070000010002
20011111111111110002
20011111111111110002
20111111111111110002
20010000000001110002
20010000000001110002
20000000000001110002
20010000100001110002
20010000001001110002
20010000000001110002
20010001000001110002
20010000000001110002
20011111111111110002
20011111111111110002
20011111111111110002
20011111111111110002
20000000000000000002
20000000000000000002
20000000000000000002
20000000000000000002
20000000004000000002
22222222222222222222
it sould be:
(whithout the , and {})
out put in array:
Code:
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,2},
{2,0,0,0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