Search code examples
c#arraysstringbinaryformatter

Saving three arrays to one file then separating the file back into three arrays


I'm making a program that moves those three arrays into one array which is then saved in a binary file. The program is then supposed to open and read the file, trim the characters that aren't supposed to be there and move the information back into the correct array. The removing characters and moving the information is what I'm having an issue with, it's currently only printing to one array. If anyone can help that would be great, the code is below (sorry for any formatting/spelling mistakes, it's late)

Saving the information:

private void BtngameSave_Click(object sender, EventArgs e)
        {
            string FileName = "mygames.dat";
            SaveFileDialog SaveBinary = new SaveFileDialog();
            DialogResult sr = SaveBinary.ShowDialog();
            if (sr == DialogResult.OK)
            {
                FileName = SaveBinary.FileName;
            }
            try
            {
                using (Stream stream = File.Open(FileName, FileMode.Create))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    for (int i = 0; i < ptr; i++)
                    {
                        save[i] = gameQueueTitle[i] +"*"+ gameQueueGenre[i] +"*"+ gameQueuePlat[i];
                        bin.Serialize(stream, save[i]);
                    }
                    MessageBox.Show("File saved");
                }
            }
            catch (IOException)
            {
                MessageBox.Show("The Save Binary Stream did not work");
            }
        }

Opening the saved file:

private void BtngameOpen_Click(object sender, EventArgs e)
        {
            string FileName = "mygames.dat";
            OpenFileDialog OpenBinary = new OpenFileDialog();
            DialogResult sr = OpenBinary.ShowDialog();
            if (sr == DialogResult.OK)
            {
                FileName = OpenBinary.FileName;
            }
            ptr = 0;
            try
            {
                using (Stream stream = File.Open(FileName, FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    while (stream.Position < stream.Length)
                    {
                        string rec = bin.Deserialize(stream).ToString();
                        gameQueueTitle[ptr] = rec;
                        ptr++;
                    }
                    SortList();
                    DisList();
                }
            }
            catch (IOException)
            {
                MessageBox.Show("Couldn't open the binary file");
            }
}

Solution

    1. I recommend to define a class that holds gameQueueTitle, gameQueueGenre, gameQueuePlat like: that would iliminate the need for 3 seperate lists and you don't have to fear a mismatch, that one list is missing a element, because of a bug or something like this. You also don't need ptr anymore, because the list has a count property:
        public class GameInfo
        {
            public string Title
            {
                get;
                set;
            }
    
            public string Genre
            {
                get;
                set;
            }
    
            public string Plat
            {
                get;
                set;
            }
        }
    
    1. To answer your questions i rewrote your functions quickly (if i understood you question correctly your code didn't work):

    Saving the information:

    private void BtngameSave_Click(object sender, EventArgs e)
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.FileName = "mygames.dat";
    
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        using (FileStream fs = new FileStream(dialog.FileName, FileMode.OpenOrCreate))
                        {
                            using (StreamWriter writer = new StreamWriter(fs))
                            {
                                for(int i = 0; i < ptr; ++i)
                                {
                                    writer.WriteLine(gameQueueTitle[i] + "*" + gameQueueGenre[i] + "*" + gameQueuePlat[i]);
                                }
                            }
    
                            fs.Close();
                        }
    
                        MessageBox.Show("File saved");
                    }
                    catch
                    {
                        MessageBox.Show("Couldn't save the binary file");
                    }
                }
            }
    

    Opening the saved file:

            private void BtngameOpen_Click(object sender, EventArgs e)
            {
                OpenFileDialog dialog = new OpenFileDialog();
    
                if (dialog.ShowDialog() == DialogResult.OK)
                { 
                    try
                    {
                        using (FileStream fs = new FileStream(dialog.FileName, FileMode.OpenOrCreate))
                        {
                            using (StreamReader reader = new StreamReader(fs))
                            {
                                ptr = 0;
    
                                for (ptr = 0; !reader.EndOfStream; ++ptr)
                                {
                                    string line = reader.ReadLine();
    
                                    string[] values = line.Split('*');
    
                                    gameQueueTitle[ptr] = values[0];
                                    gameQueueGenre[ptr] = values[1];
                                    gameQueuePlat[ptr] = values[2];
                                }
                            }
    
                            fs.Close();
                        }
    
                        MessageBox.Show("File processed");
                    }
                    catch
                    {
                        MessageBox.Show("Couldn't open the binary file");
                    }
                }
            }
    
    1. Only save or open a file if the Dialog Result is Ok. Your code saves/read the file even if the user cancels the dialog. No user expects this behavior.