#求大神解决
//文件路径
string fileName = @"D:\c#\workspace\myAssignment\chepiao2.txt";
Console.WriteLine("\n\t简单客车售票系统" + "\n");
string[,] zuo = new string[9, 4];
for (int i = 0; i < 9; i++)
{
//Console.WriteLine();
for (int j = 0; j < 4; j++)
{
zuo[i,j] = "【有票】";
//Console.Write(zuo[i, j]);
}
}
string s = string.Empty;//这个是标识退出或座位的。
//读取文件中的数据
while (true)
{
using (StreamReader sr = new StreamReader(fileName))
{
//一次读取一行
string line;
while ((line = sr.ReadLine()) != null)
{
//读取到的数据输出到控制台
Console.WriteLine(line);
}
}
Console.Write("请输入作为行号和列号(例如:0,2)按q退出!");
s = Console.ReadLine();//读取输入的值。
if (s == "q") break;
try
{
string[] str = s.Split(',');//以逗号分隔。
int one = int.Parse(str[0]);
int two = int.Parse(str[1]);
while (true)
{
if (one >= zuo.GetLength(0) || two < 0
|| one >= zuo.GetLength(0) || two < 0)
{
Console.WriteLine("输入的座位号有误请重新输入");
break;
}
else if (zuo[one, two] == "【已售】" )
{
//zuo[one, two] = "【已售】";
Console.WriteLine("输入座位票已售出,请重新输入");
//zuo[one, two] = "【已售】";
break;
}
else
{
zuo[one, two] = "【已售】";
Console.WriteLine("售票成功!");
break;
}
}
}
catch (Exception)
{
Console.WriteLine("异常错误!");
}
if (s != "q")
{
//将控制台的数据写到文件中去
using (StreamWriter sw = new StreamWriter(fileName))
{
for (int i = 0; i < 9; i++)
{
string str = " ";
for (int j = 0; j < 4; j++)
{
str += zuo[i, j] + ",";
}
sw.WriteLine(str);
}
}
}
else
{
break;
}
}
Console.ReadKey();
上次发布的可能问题描述的有点问题,试了还是没有效果,希望回答了的还可以再次回答下,拯救小白?
直接写入本来就会覆盖整个文本文件,
你需要用追加的方式(以下代码FileMode.Append就是追加的意思)
string path="D\1.txt";//文件的路径,保证文件存在。
FileStream fs=new FileStream(path,FileMode.Append);
SteamWriter sw=new StreamWriter(fs);
sw.WriteLine(要追加的内容);
sw.Close();
fs.Close();
如果你不想改这些代码,还有一种更简单的方式,就是保留第一次读取出来的数据字符串,往后面添加
var sb=new StringBuilder();
sb.Append(第一次读出来的字符串);
sb.Append(现在要写入的字符串);
然后把sb.ToString()写入到文件中就可以了。
如有不懂请继续提问
====以下为更新
前面评论说的不是很清楚,我直接改了一下代码你看看:
Console.WriteLine("\n\t简单客车售票系统" + "\n");
string[,] zuo = new string[9, 4];
for (int i = 0; i < 9; i++)
{
//Console.WriteLine();
for (int j = 0; j < 4; j++)
{
zuo[i, j] = "【有票】";
//Console.Write(zuo[i, j]);
}
}
//这段是我加的,你把程序保存在文本中的数据读取只显示到屏幕上,但是你没有把它修改到zuo数组中啊,所以票都是可售
if (File.Exists(fileName))
{
using (StreamReader sr = new StreamReader(fileName))
{
string line;
line=sr.ReadLine();
for (int i = 0; i < 9; i++)
{
bool flag = !string.IsNullOrEmpty(line);//如果数据不完整(没有该行的数据)就视为有票
string[] cols = null;
if (flag)
{
cols = line.Split(',');
}
for (int j = 0; j < 4; j++)
{
if (flag)
{
if (cols.Length < 4)//保存下来的数据该行没有4列,则报错
throw new Exception("数据不完整!");
zuo[i, j] = cols[j];
}
}
line = sr.ReadLine();
}
}
}
string s = string.Empty;//这个是标识退出或座位的。
//读取文件中的数据
while (true)
{
//不需要每次都去读取,程序没有退出,内存中的数组是不会被释放的。所以被我删掉了。
//程序打开的时候读取一次就行了
Console.Write("请输入作为行号和列号(例如:0,2)按q退出,按p查看座位情况!");
s = Console.ReadLine();//读取输入的值。
if (s == "q") break;
if (s == "p")
{
for (int i = 0; i < 9; i++)
{
StringBuilder sb = new StringBuilder();
for (int j = 0; j < 4; j++)
{
sb.Append(zuo[i, j]).Append(",");
//Console.Write(zuo[i, j]);
}
Console.WriteLine(sb.ToString());
}
continue;
}
try
{
string[] str = s.Split(',');//以逗号分隔。
int one = int.Parse(str[0]);
int two = int.Parse(str[1]);
while (true)
{
if (one >= zuo.GetLength(0) || two < 0
|| one >= zuo.GetLength(0) || two < 0)
{
Console.WriteLine("输入的座位号有误请重新输入");
break;
}
else if (zuo[one, two] == "【已售】")
{
//zuo[one, two] = "【已售】";
Console.WriteLine("输入座位票已售出,请重新输入");
//zuo[one, two] = "【已售】";
break;
}
else
{
zuo[one, two] = "【已售】";
Console.WriteLine("售票成功!");
break;
}
}
}
catch (Exception)
{
Console.WriteLine("异常错误!");
}
if (s != "q")
{
//将控制台的数据写到文件中去
using (StreamWriter sw = new StreamWriter(fileName))
{
for (int i = 0; i < 9; i++)
{
string str = " ";
for (int j = 0; j < 4; j++)
{
str += zuo[i, j] + ",";
}
sw.WriteLine(str);
}
}
}
else
{
break;
}
}
Console.ReadKey();
}
using (StreamWriter sw = new StreamWriter(fileName, true)) {
for (int i = 0; i < 9; i++)
{
string str = " ";
for (int j = 0; j < 4; j++)
{
str += zuo[i, j] + ",";
}
sw.WriteLine(str);
}
}