将三个数组保存到一个文件中,然后将文件分成三个数组
Saving three arrays to one file then separating the file back into three arrays
我正在制作一个程序,将这三个数组移动到一个数组中,然后保存在一个二进制文件中。然后程序应该打开并读取文件,trim 不应该存在的字符并将信息移回正确的数组。删除字符和移动信息是我遇到的问题,它目前只打印到一个数组。如果有人能提供帮助那就太好了,下面是代码(抱歉有任何 formatting/spelling 错误,已经晚了)
正在保存信息:
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");
}
}
- 我建议定义一个 class 来保存 gameQueueTitle、gameQueueGenre、gameQueuePlat,例如:这将消除对 3 个单独列表的需要,您不必担心不匹配,一个列表缺少一个元素,因为错误或类似的东西。您也不再需要 ptr 了,因为该列表有一个计数 属性:
public class GameInfo
{
public string Title
{
get;
set;
}
public string Genre
{
get;
set;
}
public string Plat
{
get;
set;
}
}
- 为了回答您的问题,我快速重写了您的函数(如果我理解您的问题是正确的,那么您的代码无法运行):
正在保存信息:
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");
}
}
}
- 仅在对话框结果正常时才保存或打开文件。即使用户取消对话框,您的代码 saves/read 文件也是如此。没有用户期望这种行为。
我正在制作一个程序,将这三个数组移动到一个数组中,然后保存在一个二进制文件中。然后程序应该打开并读取文件,trim 不应该存在的字符并将信息移回正确的数组。删除字符和移动信息是我遇到的问题,它目前只打印到一个数组。如果有人能提供帮助那就太好了,下面是代码(抱歉有任何 formatting/spelling 错误,已经晚了)
正在保存信息:
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");
}
}
- 我建议定义一个 class 来保存 gameQueueTitle、gameQueueGenre、gameQueuePlat,例如:这将消除对 3 个单独列表的需要,您不必担心不匹配,一个列表缺少一个元素,因为错误或类似的东西。您也不再需要 ptr 了,因为该列表有一个计数 属性:
public class GameInfo
{
public string Title
{
get;
set;
}
public string Genre
{
get;
set;
}
public string Plat
{
get;
set;
}
}
- 为了回答您的问题,我快速重写了您的函数(如果我理解您的问题是正确的,那么您的代码无法运行):
正在保存信息:
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");
}
}
}
- 仅在对话框结果正常时才保存或打开文件。即使用户取消对话框,您的代码 saves/read 文件也是如此。没有用户期望这种行为。