[Question]Truncate/Open

07/19/2010 00:32 Fish*#1
if I use(To save char)
Code:
FileMode.Truncate
then
Code:
FileMode.Open
Will it make any difference?
FileMode.Truncate will replace all right?
07/19/2010 00:43 kinshi88#2
Truncate opens the file and truncates it till the file size is zero bytes, so yes, it replaces it all.
You only need to use Truncate, as it opens it as well.
07/19/2010 01:08 Fish*#3
So I should use Truncate instead.
it would be better?
07/19/2010 01:13 kinshi88#4
Well I'm not sure how you're doing it.

Truncate opens the file and removes the contents.
Open just opens the file.

So you decide =P
07/19/2010 01:18 Fish*#5
emm, this is how it looks the code:
example:
(For open)
Code:
        public static void SaveChar(string Name)
        {
            FileStream FS = new FileStream(@"C:\Conquer\Characters\" + Name + ".chr", FileMode.Open);
            BinaryWriter BW = new BinaryWriter(FS);
            BW.Write("Accname");
            BW.Write("Location");
            BW.Write("Noob");
            BW.Write("Silvers");
            BW.Flush();
            FS.Flush();
            BW.Close();
            FS.Close();
        }
For Truncate
example:
Code:
       public static void SaveChar(string Name)
        {
            FileStream FS = new FileStream(@"C:\Conquer\Characters\" + Name + ".chr", FileMode.Truncate);
            BinaryWriter BW = new BinaryWriter(FS);
            BW.Write("Accname");
            BW.Write("Location");
            BW.Write("Noob");
            BW.Write("Silvers");
            BW.Flush();
            FS.Flush();
            BW.Close();
            FS.Close();
        }
would it make any difference.
or it would be same if i use open or truncate?
:D
07/19/2010 03:49 CptSky#6
With 'Open', you can only edit a few bytes. With 'Truncate', you need to rewrite all the file.
07/19/2010 04:21 Fish*#7
so if i use this for save, truncate will be best?
dats how i see it.
cuz open only change some things, truncate will change all.
I could be wrong? :)
07/19/2010 10:15 Korvacs#8
Depends what your writing, if you use a filestream and you write from the start of the file you overwrite the contents anyway, so really theres no difference between the two methods, you could argue that in this case .Open would be better because you arnt having the cpu truncate the file first, but the performance gain is going to be marginal.
07/19/2010 21:29 Fish*#9
Okay I will let it be Open ;D
07/20/2010 12:56 ~Falcon#10
You should employ the using operator when working with classes that inherit the IDisposable class.

That way, when the using() block closes, all resources are freed.
07/21/2010 15:39 Fish*#11
okay thanks :)