Skip to main content
gamedev.net
🔒 Locked

C++ file I/O

Started by parklife Aug 16, 2001 at 1:31 PM 4 replies 450+ views
Original Post
parklife
parklife
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*
Grib
Grib
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.

Colin Jeanne
Colin Jeanne
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]
parklife
parklife
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*
Bully
Bully
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 "

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.