• Advertisement
Sign in to follow this  

[.net] Files not saving:

This topic is 3874 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

I use the following code and am able to step through both functions without any exceptions being thrown, but when I reload the file the changes havn't been saved. Is there anything obvious that I've done wrong?
        public void Save()
        {
            Save(fileName);
        }

        public void Save(String fileName)
        {
            FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Write);
            BinaryWriter writer = new BinaryWriter(file);

            for (int i = 0; i < instanceCount; i++)
            {
                writer.BaseStream.Position = instanceStart + 0x4C * i;
                writer.Write(instanceNames.ToCharArray());
            }

            for (int i = 0; i < instanceTypes.Length; i++)
            {
                writer.BaseStream.Position = instanceTypesStart + 0x10 * i;
                writer.Write(instanceTypes.ToCharArray());
            }

            writer.Close();
            file.Close();
            writer = null;
            file = null;

            this.fileName = fileName;
        }
EDIT: It does save if I use the top method, but not the bottom one, which makes no sense because the top one calls the bottom one anyway. When I use the debugger, the correct file name is being passed into the FileStream constructor. Also, using FileMode.Open was deliberate as I only wanted to overwrite existing files. [Edited by - CodeReaver on July 15, 2007 8:28:13 AM]

Share this post


Link to post
Share on other sites
Advertisement
This seemed to work fine for me; I don't know what collection types you were using though:

<code>
public void Save(String fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(file);

int instanceStart = 5;
int instanceTypesStart = 10;
List<String> instanceNames = new List<String>();
List<String> instanceTypes = new List<String>();

instanceNames.Add("Name1");
instanceNames.Add("Name2");
instanceNames.Add("Name3");

instanceTypes.Add("Type1");
instanceTypes.Add("Type2");
instanceTypes.Add("Type3");

for (int i = 0; i < instanceNames.Count /*instanceCount*/; i++)
{
writer.BaseStream.Position = instanceStart + 0x4C * i;
writer.Write(instanceNames.ToCharArray());
}

for (int i = 0; i < instanceTypes.Count; i++)
{
writer.BaseStream.Position = instanceTypesStart + 0x10 * i;
writer.Write(instanceTypes.ToCharArray());
}

writer.Flush();

writer.Close();
writer = null;

file.Close();
file = null;

//this.fileName = fileName;
}
</code>

Share this post


Link to post
Share on other sites
I would also suggest you use the "using" keyword as it will clean up the resources for you. It's a good habit to get into with .NET.

public void Save(String fileName)
{
using(FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Write))
{
using(BinaryWriter writer = new BinaryWriter(file))
{
for (int i = 0; i < instanceCount; i++)
{
writer.BaseStream.Position = instanceStart + 0x4C * i;
writer.Write(instanceNames.ToCharArray());
}

for (int i = 0; i < instanceTypes.Length; i++)
{
writer.BaseStream.Position = instanceTypesStart + 0x10 * i;
writer.Write(instanceTypes.ToCharArray());
}
}
}

this.fileName = fileName;
}

Share this post


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

  • Advertisement