Help with using files with arrays

This is a discussion on Help with using files with arrays within the C++ Programming forums, part of the General Programming Boards category; Hi! I am trying to load the information in a file and put into an array. Can anyone tell me ...

  1. #1
    Unregistered
    Guest

    Help with using files with arrays

    Hi!
    I am trying to load the information in a file and put into an array. Can anyone tell me how I might do this? Also, how do start reading from a different line in a file? I am using fstream.h to do this. Thanks! ^_^

  2. #2
    Unregistered
    Guest
    Thanks for the getline suggestion. My file will contain lines of numbers. I'm making a map file loading engine.

  3. #3
    Unregistered
    Guest
    Someone please help!!!

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    I can help, I've done something like this before. Okay, let's say this the file (a 10x10 square):

    1 = grass
    2 = water
    3 = mountain

    2 2 2 2 2 2 2 2 2 2
    2 2 1 1 1 1 3 1 2 2
    2 1 1 1 3 3 1 3 1 2
    2 2 1 3 1 1 3 1 1 2
    2 2 2 1 1 1 3 1 2 2
    2 2 1 1 3 2 2 1 1 2
    2 1 1 3 3 2 1 3 2 2
    2 2 2 1 3 1 3 1 1 2
    2 2 2 2 2 1 1 2 2 2
    2 2 2 2 2 2 2 2 2 2

    This would require an array of 100 elements.

    Code:
    int islandMap[10][10];
    To initialize this, let's say you have a function called InitMap().

    Code:
    void InitMap()
    {
         ifstream map("island.map");
         char ch;
         for (int i=0; i<10; i++)
         {
              for (int j=0; j<10; j++)
              {
                   map >> ch;
                   islandMap[i][j] = (int *)ch;
              }
         }
    }
    Or something along those lines. I hope that helps a little. Let me know if that still won't work, I've another idea that may.

    Brendan

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Oops. In the above InitMap() function, where it says:

    Code:
    islandMap[i][j] = (int *)ch;
    Take out the *. It will work then. So it should look like this:
    Code:
    islandMap[i][j] = (int)ch;
    Brendan

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Lol oops actually none of that will work. Here, this works, and I know this because I tested it:

    Code:
    void InitMap()
    {
    	char str[100];
    	ifstream map("island.map");
    	for(int i=0;i<=10;i++)
    	{
    		map>>str;
    		for(int j=0;j<10;j++)
    		{
    			switch ((int)str[j])
    			{
    			case '1':
    				islandMap[i][j]=1;
    				break;
    			case '2':
    				islandMap[i][j]=2;
    				break;
    			case '3':
    				islandMap[i][j]=3;
    			}
    		}
    	}
    }
    A DispMap() function would look something like this:

    Code:
    void DispMap()
    {
    	for (int i=0; i<10; i++)
    	{
    		for (int j=0; j<10; j++)
    		{
    			switch (islandMap[i][j])
    			{
    			case 1:
    				cout << "-";
    				break;
    			case 2:
    				cout << "~";
    				break;
    			case 3:
    				cout << "^";
    				break;
    			}
    		}
    		cout << "\n";
    	}
    }
    Hope that helps!

    Brendan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming Help - Using Arrays and Files
    By samdog45 in forum C Programming
    Replies: 1
    Last Post: 03-10-2008, 03:18 AM
  2. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 01:59 AM
  3. arrays and files
    By c++.prog.newbie in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2004, 01:00 PM
  4. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 12:42 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM

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