Vector fstream outputting

This is a discussion on Vector fstream outputting within the C++ Programming forums, part of the General Programming Boards category; I'm trying to load a map from a file into the vector and then printing it back out, but I'm ...

  1. #1
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12

    Smile Vector fstream outputting

    I'm trying to load a map from a file into the vector and then printing it back out, but I'm not sure about how to do it. I'm trying to make a map where the user can walk and he can't walk into walls. Map looks something like this:

    XXXXXXaaaa
    XXXXXXaaaa
    XXXXXXaaaa
    XXaaXXXXaa
    XaaaaXXXaa
    Xaaaaaaaaa
    XXaaaXXXXX
    XXXaXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX

    It's supposed to be 10X10.

    Any help is greatly appreciated.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    So... you have a vector<char[10]> container?

    1. Read a line of data from the file into a buffer.
    2. Call the vector's push_back member function to load that line of data into the vector.
    3. Repeat until no more lines of data in the file.

    Are you having trouble with something specific? File I/O confusing you? Not sure about the ins/outs of the vector container?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    I'm having trouble creating my display function where it displays the map when I initialize it.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Since the data is in a vector, what does a map have to do with it?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by 7stud
    Since the data is in a vector, what does a map have to do with it?
    The vector contains the "map" (note: not a STL map container) that is to be walked through. Better refered to as a maze perhaps?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    Yeah, I'm trying to print this maze using a function but im confused on how to do it with a vector.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by azndragon247
    Yeah, I'm trying to print this maze using a function but im confused on how to do it with a vector.
    Here's one way, which uses array notation with a vector:
    Code:
    #include<vector>
    #include<string>
    using namespace std:
    ..
    ..
    ..
    string str1 = "my maze row 1";
    string str2 = "my maze row 2";
    
    vector<string> myVector(2);
    myVector[0] = str1;
    myVector[1] = str2;
    
    for(int i = 0; i<myVector.size(); i++)
    {
    	cout<<myVector[i]<<endl;
    }
    Last edited by 7stud; 06-08-2005 at 05:23 PM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    you should make a map abstraction, it may look like this
    Code:
    class map
    {
        typedef char value_type;   
    public:
        void load(const char *file)
        {
             ifstream ifs(file, ios_base::binary);
             width_ = readfrom_ifs;
             height_ = readform_ifs;
             copy(istream_iterator<value_type>(ifs), istream_iterator<value_type>(), back_inserter(buffer_)); 
        }
    
        void print(ostream & os)
        {
             for(int i = 0; i < height_; i++)
             {
                 for(int j= 0; j<width_; j++ )
                 {
                         os<<buffer_[i*width_ + j];
                 }
                 os<<'\n';
             }
        }
    private:
        int width_, height_;
        vector<value_type> buffer_;
    };
    then, you get width_ and height_ hence you can walk on the map
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sounds like you're just trying to print the contents of the vector? Try something like:
    Code:
    std::copy(std::ostream_iterator(std::cout), maze.begin(), maze.end());
    Not sure if I got the right arguments for ostream_iterator (or if it's really called that), or if I got the right arguments for std::copy() either But I bet it's close, since that's one of hk_mp5kdw's stock replies (or something close, involving ostream_iterator and the copy algorithm).

    Of course, if you're just looking for a solution that works (and is easily read and understood by your average coder), then go with something like 7stud's solution. For loops are generally more widely understood than mucking around with <algorithm> and whatever header ostream_iterator is defined in
    Last edited by Hunter2; 06-08-2005 at 05:49 PM.
    Just Google It. √

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

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    For a vector if ints you would do this:
    Code:
    std::copy(maze.begin(), maze.end(), std::ostream_iterator<int>(std::cout, "\n"))
    But not a vector of char[10].
    How to accomplish the task correctly depends on the code of the OP. Actual code should be posted, like the declaration of the vector that is to be output.

  11. #11
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    So i would have height_ and width_ in the maze file to be loaded?

  12. #12
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    /*READ A FILE STORE AS A 2D 10X10
      MATRIX*/
    
    
    
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <conio.h>
    #include <fstream>
       
    using namespace std;
    int main()
    {
        
         char array[81];
         char matrix[81][81];
         
         SetConsoleTitle("T R E E N E F");
         fstream file_pointer;
         file_pointer.open("maze.txt",ios::in);
         
         int i=0;
           do{
               file_pointer>>array;
               int size=strlen(array);
               cout<<array<<endl;
               for(int j=0; j<size; j++)
               {
                   matrix[i][j]=array[j];
               }
               i++;
             }while(file_pointer.peek()!=EOF);
             
             int x;
             int y;
             
             cout<<"Input x coord>>";
             cin>>x;
             cout<<"Input y coord>>";
             cin>>y;
             
             cout<<matrix[x-1][y-1];
             
             int stop;
             cin>>stop;
    }
    Attached Files Attached Files

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>But not a vector of char[10].
    Right, my mistake. I saw char[10] and figured it would be a null-terminated string.

    azndragon247, before we go any further: Are you having problems loading the maze, or are you having problems displaying it, or both?

    And if you're having problems with anything, can you post the section of your code that you're having problems with? It's hard finding out a problem if we can't see the code.
    Just Google It. √

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

  14. #14
    Registered User azndragon247's Avatar
    Join Date
    Jun 2005
    Posts
    12
    Both actually, but heres the code for the loading function and

    In the .h file:

    Code:
    #include <stdlib.h>
    #include <vector>
    #include <fstream>
    
    class CMap
    {
    	char matrix [10][10];
    	vector <char> Map;
    	int char_x;
    	int char_y;
    
    public:
    
    	CMap();
    	void display();
    	void move_player( char choice );
    	void loadmap( istream &inStream );
    	void savefile( ostream &outStream );
    };
    In the .cpp file:
    Code:
    #include "CMap.h"
    #include <iostream>
    #include <stdlib.h>
    #include <vector>
    #include <fstream>
    #include <stdio.h> 
    #include <conio.h> 
    
    
    using namespace std;
    
    CMap::CMap()
    {
    	this->loadmap();
    }
    
    void CMap::move_player()
    {
    	enum
    	{
      		UP_ARROW    = 256 + 72,
      		DOWN_ARROW  = 256 + 80,
      		LEFT_ARROW  = 256 + 75,
      		RIGHT_ARROW = 256 + 77
    	};
    
    	static int get_keys( void )
    	{
    		int key = getch();
    
    		if ( key == 0 || key == 224 )
    			key = 256 + getch();
    
    		return key;
    	}
    
    	while ( key = get_keys() )
    	{
    		switch ( key )
    		{
    		case UP_ARROW:
    		if 
    			cout << "Going up." << endl;
    		break;
    		case DOWN_ARROW:
    		if
    			cout << "Going down." << endl;
    		case LEFT_ARROW:
    		if
    			cout << "Going left." << endl;
    		break;
    		case RIGHT_ARROW:
    		if
    			cout << "Going right." << endl;
    		break;
    		}
    	}
    
    void CMap::save()
    {
    	ofstream savefile( "Save.txt" );
    	vector <char>::iterator map_Iter;
    	for ( map_Iter = map.begin(); map_Iter != map.end(); map_Iter++ )
    	{
    		 savefile << *map_Iter;
    	}
    }
    
    
    void CMap::display()
    {
    	for (int i=0; i<Map.size(); i++)
    	{
    		for(int j=0; j<Map.size(); j++)
               	{
                   		matrix[i][j]=Map[i];
               	}
    	cout << matrix[i][j];
    	
    }
    
    void CMap::loadmap()
    {
    	ifstream loadfile;
    	loadfile.open("Level1b.txt");
    	for ( int i = 0; i < 100; i++ )
    	{
    		 loadfile >> Map[i]; 
    	}
    }
    Currently i still need to work out how to print the players location and the exit. And also how to check for walls (X's).

    Plus as a added note, I would like to thank all the people who replied to this thread. Your examples have really helped me to understand some stuff.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In your map constructor, you call loadmap, which changes the value of the first 100 entries in the map vector. Unfortunately for you, at the time you are trying to change the value in the vector, the vector is empty. You are accessing data beyond the bounds of the vector, and asking for a crash.

    Construct your vector with 100 members (or whatever you feel is appropriate) or use push_back when you add data to the vector (e.g. char c; loadFile >> c; Map.push_back(c);).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash Table outputting incorrect number
    By Paul Skinner in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:19 AM
  2. Problem with outputting to a file
    By LowlyIntern in forum C Programming
    Replies: 6
    Last Post: 08-14-2008, 02:55 PM
  3. Inputting and outputting from structs
    By ashcan1979 in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2006, 04:31 AM
  4. Multiple structs and outputting them
    By Cstudent2121 in forum C Programming
    Replies: 7
    Last Post: 12-17-2005, 08:26 AM
  5. Outputting a string to the screen
    By Soopafly in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2002, 02:11 PM

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