Thread: reading multidimensional array from a file

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    reading multidimensional array from a file

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    #define MAP_WIDTH 70
    #define MAP_HEIGHT 22
    
    void readMap(int map[MAP_WIDTH][MAP_HEIGHT])
    {
    	ifstream fin;
    	fin.open("test.txt");
    
    	for(int i = 0; i < MAP_WIDTH; i ++)
    	{
    		for(int t = 0; t < MAP_HEIGHT; t ++)
    			fin >> map[i][t];
    	}
    
    	fin.close();
    }
    
    void dispMap(int map[MAP_WIDTH][MAP_HEIGHT])
    {
    	for(int i = 0; i < MAP_WIDTH; i ++)
    	{
    		for(int t = 0; t < MAP_HEIGHT; t ++)
    		{
    			if(map[i][t] == 0)
    				cout << ' ';
    			else if(map[i][t] == 1)
    				cout << '.';
    			else if(map[i][t] == 2)
    				cout << '#';
    		}
    
    		cout << endl;
    	}
    }
    
    int main()
    {
    	int map[MAP_WIDTH][MAP_HEIGHT];
    
    	readMap(map);
    	dispMap(map);
    
    	return 0;
    }
    When I run this program all that shows up are many blank lines. Any help would be appreciated.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to read the file as chars rather than ints, and convert each char to an int yourself

    At present, each row is taken as being just one int, either zero with lots of leading zeros, or something rather large, with lots of leading zeros.
    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.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You could also put spaces between each character, I think.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Does a multidimensional array not read accross and then skip down a line?
    If so I think your loops are back to front. Should the height not be specified first and then traverse the inner loop along the width???
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Good call eth0!
    The map itself is also wrong then, it should be
    int map[MAP_HEIGHT][MAP_WIDTH];
    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.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    11
    Oh, I'm interested in your programme, too. Would you please post the finally version of your programming source code here, thx.

    By the way, would you mind writing a programme that requires few 2-D character arrays from user input, then store those arrays in a text file? I think, to work this programme out, you should place some identifier or say a divider(e.g. ' | ') in that text file between each 2-D array....and so on...
    Last edited by choykawairicky; 05-13-2004 at 09:44 AM.

  7. #7
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Thanks for the help guys, here's my finished code

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    #define MAP_WIDTH 70
    #define MAP_HEIGHT 22
    
    void readMap(char map[MAP_HEIGHT][MAP_WIDTH])
    {
    	ifstream fin;
    	fin.open("test.txt");
    
    	for(int i = 0; i < MAP_HEIGHT; i ++)
    	{
    		for(int t = 0; t < MAP_WIDTH; t ++)
    			fin >> map[i][t];
    	}
    
    	fin.close();
    }
    
    void dispMap(char map[MAP_HEIGHT][MAP_WIDTH])
    {
    	for(int i = 0; i < MAP_HEIGHT; i ++)
    	{
    		for(int t = 0; t < MAP_WIDTH; t ++)
    		{
    			if(map[i][t] == '0')
    				cout << ' ';
    			else if(map[i][t] == '1')
    				cout << '.';
    			else if(map[i][t] == '2')
    				cout << '#';
    		}
    
    		cout << endl;
    	}
    }
    
    int main()
    {
    	char map[MAP_HEIGHT][MAP_WIDTH];
    
    	readMap(map);
    	dispMap(map);
    
    	return 0;
    }
    Works nicely
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. reading a file into an array
    By dantegl36 in forum C Programming
    Replies: 11
    Last Post: 12-02-2006, 12:23 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Reading Characters from file into multi-dimensional array
    By damonbrinkley in forum C Programming
    Replies: 9
    Last Post: 02-24-2005, 01:31 PM