Click here to Skip to main content
15,878,029 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
can any one please tell me how to store 2-D array in a text file using c#

Updated 9-Feb-12 22:58pm
Sergey Alexandrovich Kryukov 10-Feb-12 4:58am    
Why? Why in a text file?!
k.Prathap 10-Feb-12 5:00am    
i need to store my 2D depth array data into a text file.

Why? Just use Data Contract and all will be stored and loaded for you automatically and without much of your effort. As a bonus, you will see how a cultured and well-readable XML or JSON storage looks like.

Please see:[^].

See also my past answers on the topic:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

Share this answer
k.Prathap 10-Feb-12 5:03am    
actually i want to store my 2d depth array data in a text file. I am doing the project on kinect
Sergey Alexandrovich Kryukov 11-Feb-12 5:43am    
I say, use Data Contact. I will store/load it in a text file. XML and JSON are text file. But if you want to develop your own persistence -- you got my approval, go ahead. But "I want" without any motivation hardly can impress anyone... :-)
Espen Harlinn 11-Feb-12 5:14am    
Sergey Alexandrovich Kryukov 11-Feb-12 5:41am    
Thank you, Espen.
Store each row of the array on a separate line. All columns of a row will thus have to placed on one line. Separate each of the column values with a suitable separator, i.e. a character or a string that cannot be part of any given value. If your array only contains numbers the choice of the column separator is easier to solve, just take a space or a tab character. Still begs the question what you are trying to achieve by that, but that can only answered by yourself. :)


Share this answer
If you want to store the values in a human-friendly format (since you are using text file, I assume that) the you may write 'verbosely' each item, e.g.
a[0][0] = 5
a[0][1] = 7
a[1][0] = 27
a[1][1] = -7

(see How to: Write to a Text File[^]).

On the other hand, if you need to store the array for later retrieval in you program then you may prefer serialization[^].
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900