944,162 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 8148
  • C# RSS
Mar 3rd, 2007
0

How do I work with data files?

Expand Post »
So here's the deal. I learned to program with python, and C# is my second step up the ladder. I'm currently learning the language by making a Sudoku game (just porting over the code from python..nothing fancy).

So I ran into a dead end just now...I know how to manipulate data files {*.dat) in python, to store objects/arrays/ etc. How is this done in C#? I need a quick way to open a file, and put/get two objects in/from it.


P.S. is shelving available in C# as well, because something like that is what I'm talking about. (It says somewhere in the python docs or something that shelving[what I'm used to using] is domething from C, or C++[memory is foggy])
Similar Threads
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Mar 3rd, 2007
0

Re: How do I work with data files?

are you trying to write objects to a file?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 3rd, 2007
0

Re: How do I work with data files?

yes write and read
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Mar 3rd, 2007
0

Re: How do I work with data files?

Oh right. Erm I never do such things. I just read in a normal text file and that parse that data into my class or structure.

Since I don't do it I won't be offering any advice. Good luck though.

http://www.codeproject.com/csharp/objserial.asp
Last edited by iamthwee; Mar 3rd, 2007 at 7:17 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 4th, 2007
0

Re: How do I work with data files?

Scru,

I think what you are after is Serialization (known as pickling in python).

Take a look at this website:
http://blog.kowalczyk.info/kb/serial...n-in-c%23.html

-- Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Mar 6th, 2007
0

Re: How do I work with data files?

God you are fond of python ain't ya, mentioned all the time lol. Treat C# like a completely different language, don't try to make comparisons as that just leads to trouble:

StreamReader reader = new StreamReader("MyFile.dat");
string strAllFile = reader.ReadToEnd().Replace("\r\n", "\n").Replace("\n\r", "\n");
string[] arrLines = strAllFile.Split(new char[] { '\n' });
textBox1.Text = arrLines[0];
textBox2.Text = arrLines[2];

That is reading line one from the .dat file and putting it into textbox1, and then reading line 3 and putting that in textbox2 using array.

StreamWriter writer = new StreamWriter("MyFile.dat");
writer.WriteLine("Hello");
writer.WriteLine("World");

That is writing hello and world to two lines in a .dat file.
Reputation Points: 70
Solved Threads: 4
Junior Poster
RwCC is offline Offline
173 posts
since Jan 2006
Mar 6th, 2007
0

Re: How do I work with data files?

Yes I love python, lol. It's quite easy.

You guys are right though: all I had to do was look at it differently to get comfortable, and I am now. I've actually come to love some of C#s features, like the strong typing (for some reason...maybe I just like declaring variables and stuff...xD), and the intellisense (even though I think that's an IDE feature).

Oh, and that Resource file thing rocks too.

I'll try that dat file thingie and let you know how it goes.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Mar 8th, 2007
0

Re: How do I work with data files?

thank you this works
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: cryatal report very urgent
Next Thread in C# Forum Timeline: xml web configuration file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC