Help with streams

This is a discussion on Help with streams within the C++ Programming forums, part of the General Programming Boards category; Why dose this not work: Code: char array[20][26]; ifstream arrayfile ( name ); if (arrayfile.fail()) { cout << "Problems opening ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    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.
    Attached Files Attached Files
    Last edited by IM back!; 08-09-2009 at 05:32 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Your output adds newlines - arrayfile<<endl;
    But your input does nothing to remove them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    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,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,7,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,4,2},
    	{2,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,2},
    	{2,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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},

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  2. input/output streams
    By cybernike in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 11:15 PM
  3. dumb question about streams
    By terracota in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2004, 12:14 PM
  4. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 04:32 AM
  5. File Streams, new error on me.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 07:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21