5 Replies - 1099 Views - Last Post: 18 August 2009 - 04:53 PM Rate Topic: -----

#1 vfrrick   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-August 09

Trying to understand FileStream

Post icon  Posted 18 August 2009 - 03:49 PM

Can someone explaine a few thing for me I thought I undsood FileStream and then I find there are a lot of ways to use it and somethings you think would work don't. So I don't fully understand them.

I have this little snippet that I have been playing with to see how and why things work.
 FileStream strm;
			try
			{
				strm = new FileStream(fpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
			}
			catch (Exception e)
			{
				Console.WriteLine (e);
				Console.WriteLine ("Cannot open StrmSeek.txt " +
								   "for reading and writing");
				
				return;

This works but everytime it clears out the file and start fresh? And this does not work.

  
			try
			{
			   FileStream strm = new FileStream(fpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
			}
			catch (Exception e)
			{
				Console.WriteLine (e);
				Console.WriteLine ("Cannot open StrmSeek.txt " +
								   "for reading and writing");
				
				return;



Don't rememeber the exact error but this will throught a lot in the rest of the code.
And with either example my snippet does not have this.
strm.Close();
Does it matter to close it or not.

Thanks for any help in making this readable.

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to understand FileStream

#2 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Re: Trying to understand FileStream

Posted 18 August 2009 - 04:17 PM

It does matter to call Close, and you always should put it in whever your done using the stream.

From now on please your [code] [ /code] tags around your code. It makes it much more readable.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Trying to understand FileStream

Posted 18 August 2009 - 04:19 PM

It would really help if we knew the exact error message this code gives you :)

When asking a question here are some things you may want to include to help those who are trying to help you :)
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do

Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Re: Trying to understand FileStream

Posted 18 August 2009 - 04:21 PM

Also from what i can tell all your doing is opening it, and catching the file not found exception. Which its alot easier to call File.Exists("somePath"), if thats all your checking
Was This Post Helpful? 0
  • +
  • -

#5 vfrrick   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-August 09

Re: Trying to understand FileStream

Posted 18 August 2009 - 04:26 PM

Yes it test if exist and if not it will create. My question is why does it not append to the file and over rights it everytime and what is the difference between

FileStream strm;
and

FileStream strm = new FileStream(fpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
Was This Post Helpful? 0
  • +
  • -

#6 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Re: Trying to understand FileStream

Posted 18 August 2009 - 04:53 PM

View Postvfrrick, on 18 Aug, 2009 - 05:26 PM, said:

FileStream strm;
and

FileStream strm = new FileStream(fpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);


FileStream strm;
you only delcare the variable

FileStream strm = new FileStream(fpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
you actually instantiate it.

and if you want to append off an already created file, try FileMode.Append

This post has been edited by SwiftStriker00: 18 August 2009 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1