Jump to content
  • Advertisement
Sign in to follow this  
ilavos

Just wondering

This topic is 5475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you save arrays in files?Just the same way has char's or is there some kind of difference.I mean,do you have to pass the max size of the array along with it,and if you do,how?Thanks Peace

Share this post


Link to post
Share on other sites
Advertisement
You could just cycle through the file filling an array until you hit the EOF as you read it? How are you saving your arrays, anyway? Or are you talking about character arrays? What language?

Share this post


Link to post
Share on other sites
Language is C.
And no for thew endofile part.I mean thy're going to be many arrays.
So eof wont tell me where one ends and another one starts.How could that be done?

Peace

Share this post


Link to post
Share on other sites
Well, if you're dealing with numeric arrays, and you know that each array only contains values in the range 0-255, you could piece together a file like this:
0,5,3,1,5,3,-1,43,1,4,43,1,-1,4,3,1,4,1,4,-1
...where the -1 signifies "end of array".
Or, you could just start each list of variables with a variable that contains the number of elements, like:

5,1,6,4,1,3,2,1,3,10,1,2,3,4,5,6,7,8,9,10,3,5,3,1
^ ^ ^ ^
| | |_ arr 3 |
arr 1 arr 2 arr 4

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
...array only contains values in the range 0-255... where the -1 signifies "end of array".
For the char type, you can't represent both 255 and -1. If you have an unsigned char, -1 is 255, and if you have a signed char, then you can only count as high as 128.

Friendly heads up.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by benryves
...array only contains values in the range 0-255... where the -1 signifies "end of array".
For the char type, you can't represent both 255 and -1. If you have an unsigned char, -1 is 255, and if you have a signed char, then you can only count as high as 128.

Friendly heads up.


I know, so these are char arrays? (or similar single-byte arrays). In which case ignore my previous ramblings, and use a system where you have the first two bytes of record being the length (this assumes arrays are going to be above 255 bytes in length. If not just use a single byte).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
They're integer array's.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry bout the anonymous poster thing,i'm not home and dont know the password so i cant login.
Anyways,i didnt have time to fully explain tyhe problem.You see the thing is,even if these are integer arrays,i dont know the sizes of these arrays so passing the maximum value is possible in a sense,but i cant make a program with a predetermined max size for the arrays because these will vary.I'm only passing two arrays,one with integers and one with floats.Any comment?
Peace

Share this post


Link to post
Share on other sites
well if it's actually an array, you should know the size...but if it's an array that isn't full, or it's a list or something similar, you could just keep track of how many elements are in the array/list and then do something like what benryves said. You could also write a byte/series of bytes before each item that marks what it belongs to etc, but that's really inefficient and just a bad idea.

We really need more info about what you're trying to do, because it doesn't seem like there should be any problem. If your problem is writing multiple arrays and then being able to tell where one ends and another begins when you read them, just write some kind of delimeter to the file after you've written each array. It can be anything, just make it some kind of invalid value so you can check for it. If you don't want to do that, or there is no real invalid value you could use, you can prefix each set of array data with the number of items it contains - there is no way you can't know how many items it has, or can't find out easily. If it's an array it has a specific size, and if you don't use the entire array, have a var keep track of its size. If it's a list, either do the same thing and use a var to keep track of its size, or loop through each item before you write the list and 'count' them.

To be more specific than that, we're going to need some code or at least some details about what you're doing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!