Advertisement

C++ file I/O

Started by August 16, 2001 01:31 PM
4 comments, last by parklife 23 years, 6 months ago
So, how would I fix the following code to work? I _can_ get it to work if I change the definition: char* city[2][11]; to: char city[2][11][21]; and some other minor changes in code. The thing is I don''t want to do this. Please help!
  
// testly.cpp

//


#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

void SaveMap (char *filnamn);
void LoadMap (char *filnamn);

char* city[2][11] =
{
	{  //                    //

		"LAYER1LAYER1LAYER1LA",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"LAYER1LAYER1LAYER1LA",
	//                    //

	},
	{  //                    //

		"LAYER2LAYER2LAYER2LA",
		"                    ",
		"                    ",
		"     korv           ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"                    ",
		"LAYER2LAYER2LAYER2LA",
	   //12345678901234567890//

	}
};


int main(int argc, char* argv[])
{
//Display map

for(int Layer=0;Layer<2;Layer++)
	for(int y=0;y<11;y++)
		cout << city[Layer][y] << "\n";

SaveMap("korvboll.txt");
cout << "\n" << "::loading saved file::" << "\n\n";
LoadMap("korvboll.txt");

//Display loaded map

for(Layer=0;Layer<2;Layer++)
	for(int y=0;y<11;y++)
		cout << city[Layer][y] << "\n";
return(1);
};		

void SaveMap (char *filename)
{
	ofstream fout(filename);
	for(int Layer=0;Layer<2;Layer++)
	for(int y=0;y<11;y++)
		fout << city[Layer][y] << "\n";
	fout.close();
}

void LoadMap (char *filename)
{
	char name[21];
	ifstream fin(filename);
	for(int Layer=0;Layer<2;Layer++)
	for(int y=0;y<11;y++)
		fin.getline(city[Layer][y],21);
	fin.close();
}

  
/john *Save GameDev - donate*
Works fine for me, except that you may want to use vector and string (and getstring). You are also returning 1 from main, indicating an error occured, this might be a bit pessimistic.
Dos/windows never checks anyway.

Advertisement
The reason it doesn''t work is because you have an array of 2 arrarys of 11 character arrays.

char city[2][11][21];

Is the correct way to do it because you have the 2 representing how many layers there are, the 11 representing how many lines there are, and the 21 representing how many columns (characters in each line) there are.

char city[z][y][x] is your coordinate system so you dont only want to use char city[z][y]
Grib:

When I compile and run I get an access violation in LoadMap (the cin.getline-line).

--

Moreover, I''ve never really used vectors (or have i?) and strings. ... help?

/john
*Save GameDev - donate*
What he means by vectors is the STL array equivalent which are called vectors. Very powerful stuff indeed.
" The fastest code is the code you don't call "
ok here is what is going on...

when you do it like that, the compiler points to the TEMPORARY memory that was allocated in order to hold the text you had. then when you try to access it, its not there because the memory is TEMPORARY!!! that is why you need the a 3D array and not a 2Darray, so it can ALLOCATE AND KEEP the memory and not just ALLOCATE TEMPORARILY.

hope that helps a little

This topic is closed to new replies.

Advertisement