10 Replies - 4130 Views - Last Post: 14 May 2012 - 01:21 PM Rate Topic: -----

#1 nathaniscool98   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 12

Writing a 2d array to a file

Posted 14 May 2012 - 12:09 PM

Hello currently I have a 2d array which is read in from a file and displayed on screen.
next I want the user to pick an x and y axes which will then change the data, this is what I have so far,but it doesn't seem to write, so could someone please give me guidance as to where I am going wrong?


void Location::picklocation(void)
				{
	viewlocations();
	char x,y;
	cout << "\nEnter a location number to continue.";
	cin >> x >> y;	

	if (locationPlan [x][y] == '@')
	{
		cout << "Sorry this location is already taken!";
	}

	else locationPlan [x][y] = '@';
	
ofstream myFile ("location.txt", ios::in); // Reading text file into 2d array
//char locationPlan [2] [5];   
{
	for (int x = 0; x<2; x++)
{
	for (int y = 0; y < 5; y++)
	{
		myFile << locationPlan[x][y];
	}
}
myFile.close();
} 

locationPlan();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Writing a 2d array to a file

#2 aresh   User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 12:24 PM

Huh ?? Why are you first reading the input from user and then reading the values of loactionPlan ?? You should first read that, and then check if a specific location in filled. Otherwise it will contain garbage values.
Was This Post Helpful? 0
  • +
  • -

#3 simeesta   User is offline

  • Deadly Ninja


Reputation: 221
  • View blog
  • Posts: 594
  • Joined: 04-August 09

Re: Writing a 2d array to a file

Posted 14 May 2012 - 12:25 PM

What are the braces "{}" on line 17 and 26 for? Your comment on line 15 says you're reading but you're writing. Very confusing. It looks like you've copied and pasted it from somewhere.

This post has been edited by simeesta: 14 May 2012 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 nathaniscool98   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 12:32 PM

Sorry i'm not reading in, I took my reading code from another class in hope to turn it into writing code.

The brackets do nothing, sorry I took the code out of my project which has nothing to do with my question to avoid confusion but left those brackets and caused confusion!

I hope that clears things up?
Was This Post Helpful? 0
  • +
  • -

#5 aresh   User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 12:36 PM

Instead of declaring x & y to be char, declare them to be int.
Was This Post Helpful? 1
  • +
  • -

#6 nathaniscool98   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 12:49 PM

Thanks for the tip, it's still not writing to the file though, any other tips?
Was This Post Helpful? 0
  • +
  • -

#7 simeesta   User is offline

  • Deadly Ninja


Reputation: 221
  • View blog
  • Posts: 594
  • Joined: 04-August 09

Re: Writing a 2d array to a file

Posted 14 May 2012 - 12:53 PM

Check the file is open.
Was This Post Helpful? 0
  • +
  • -

#8 aresh   User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 12:54 PM

Change the open mode in line 15.. ios::in can only read, while you want to write.. Change it to ios::out to write to it.
Was This Post Helpful? 0
  • +
  • -

#9 nathaniscool98   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 01:08 PM

Yes you are right, hmm but it's still not replacing the character of the entered location to a @

I really can't think what else could be wrong, but then again I missed that obvious last thing.

Can you think what else could be wrong?
Was This Post Helpful? 0
  • +
  • -

#10 aresh   User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 01:12 PM

Is it not even showing the characters in your location.txt file ?? If it is showing the characters but not replacing them, you can try over-writing the contents. Try calling viewLoactions() (I am assuming it prints the contents of array) once more before writing to file, so you know what is wrong.
Was This Post Helpful? 0
  • +
  • -

#11 nathaniscool98   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-May 12

Re: Writing a 2d array to a file

Posted 14 May 2012 - 01:21 PM

It is working once I cleared the file and started again, it for some reason worked afterwards.

thanks for all your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1