Recent

Author Topic: Saving multi-dimensional array to file?  (Read 5882 times)

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Saving multi-dimensional array to file?
« on: August 13, 2015, 08:51:34 pm »
I've gotten to the point where most of the functionality of the first part of the software is done, the last thing is being able to load/save data.

What's a simple way of saving a multi-dimensional array of strings to file, and read that file into an array again?

derek.john.evans

  • Guest
Re: Saving multi-dimensional array to file?
« Reply #1 on: August 13, 2015, 08:53:10 pm »
What do you mean by multi-dimensional array of strings?


eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Saving multi-dimensional array to file?
« Reply #2 on: August 13, 2015, 08:55:56 pm »
array[1..10, 1..10, 1..10] of string....
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Saving multi-dimensional array to file?
« Reply #3 on: August 13, 2015, 08:57:18 pm »
servers: array[1..100] of array[1..8] of string;

derek.john.evans

  • Guest
Re: Saving multi-dimensional array to file?
« Reply #4 on: August 13, 2015, 08:57:30 pm »
Are the arrays dynamic? or static in size

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Saving multi-dimensional array to file?
« Reply #5 on: August 13, 2015, 09:01:29 pm »
Dynamic, but I can go static if that's easier.

derek.john.evans

  • Guest
Re: Saving multi-dimensional array to file?
« Reply #6 on: August 13, 2015, 09:04:47 pm »
Geez, funny how such a simple problem yields so many possibilities.

Eek!

To tell you the truth. The answers are going to be all over the place.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Saving multi-dimensional array to file?
« Reply #7 on: August 13, 2015, 09:07:02 pm »
there is no magic bullet that can help you save everything with one command if that is what you are after. Think of a string as a pointer to a dynamic array of chars. You need to use loops and access each string individually to save it.Change the strings to shortstring and things might become easier but not by much.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Saving multi-dimensional array to file?
« Reply #8 on: August 13, 2015, 09:11:18 pm »
Since this is the Beginners section, this might give you some ideas:
http://wiki.lazarus.freepascal.org/File_Handling_In_Pascal
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Saving multi-dimensional array to file?
« Reply #9 on: August 13, 2015, 09:19:57 pm »
Not really looking for "magic bullet" solutions, just solutions.

I'm definitely a Pascal beginner, and this is the beginner section.

A trick we use in the CFML world is to serialize complex structures into JSON strings, which can be loaded and saved as any string data, and then deserialized into complex structures again.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Saving multi-dimensional array to file?
« Reply #10 on: August 13, 2015, 09:26:23 pm »
1 way is to first write the array size numbers in file, and then each string. When loading, read the size numbers, allocate the whole array and read strings.

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Saving multi-dimensional array to file?
« Reply #11 on: August 13, 2015, 10:19:18 pm »
I went simple and straight forward, nested loops and linewrites/reads, works like a charm but not horribly sturdy if someone tries to modify the file by hand (Which they never should anyway).

With that working, part 1 of this project is done   :)

Some serious thank-yous to all of you.

When finished, this project will go up open source on GitHub
« Last Edit: August 13, 2015, 10:23:03 pm by Trenatos »

 

TinyPortal © 2005-2018