【问题标题】:Saving three arrays to one file then separating the file back into three arrays将三个数组保存到一个文件中,然后将文件分成三个数组
【发布时间】:2019-09-24 15:23:58
【问题描述】:

我正在编写一个程序,将这三个数组移动到一个数组中,然后将其保存在一个二进制文件中。然后程序应该打开并读取文件,修剪不应该存在的字符并将信息移回正确的数组中。删除字符和移动信息是我遇到的问题,它目前只打印到一个数组。如果有人可以提供帮助,那就太好了,代码如下(抱歉,任何格式/拼写错误,太晚了)

保存信息:

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");
            }
        }

打开保存的文件:

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");
            }
}

【问题讨论】:

  • 与其进行所有的复制和移动,不如定义一个包含 3 个列表(或数组)的类并将其序列化。完成。
  • rec.Split('*')

标签: c# arrays string binaryformatter


【解决方案1】:
  1. 我建议定义一个包含 gameQueueTitle、gameQueueGenre、gameQueuePlat 的类,例如:这将消除对 3 个单独列表的需要,并且您不必担心不匹配,因为一个列表缺少一个元素,因为一个错误或类似的东西。您也不再需要 ptr,因为该列表具有 count 属性:
    public class GameInfo
    {
        public string Title
        {
            get;
            set;
        }

        public string Genre
        {
            get;
            set;
        }

        public string Plat
        {
            get;
            set;
        }
    }
  1. 为了回答您的问题,我快速重写了您的函数(如果我理解您的问题正确,您的代码不起作用):

保存信息:

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");
                }
            }
        }

打开保存的文件:

        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. 仅当对话结果正常时才保存或打开文件。即使用户取消对话框,您的代码也会保存/读取文件。没有用户期望这种行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多