C#如何逐行读取文本文件中内容并存储到二维数组中

俺老刘 2012-08-07 11:15:27

我有个文本文件student.txt,里面存放的数据如下

张三|80|90|85
李四|82|95|74
王五|79|80|95

分别代表张三三门功课的成绩 数学,语文,英语

我想最后得到一个二维数组,可以方便调用里面的任何数据,例如某个同学的数学成绩
...全文
1221 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
狼王2012 2014-11-02
  • 打赏
  • 举报
回复
俺老刘 2012-08-10
  • 打赏
  • 举报
回复
搞定了,刚才看明白问题在哪里了
我用了个if语句,在里面return退出循环,循环后面的都不执行了。应该是break
还有就是 cmbStudent.SelectedIndex = 0;
sr.Dispose();
sr.Close();
fs.Dispose();
fs.Close();

应该把第一句放在最后。
俺老刘 2012-08-10
  • 打赏
  • 举报
回复
你的代码可以,我的就存在问题,第一次运行没问题,多选几次就会出错。
你能帮我调试一下代码吗,把我的代码复制进去,在窗体上加几个控件就行了
俺老刘 2012-08-10
  • 打赏
  • 举报
回复
我搞错了,先Dispose,然后close
dongfangshang2 2012-08-09
  • 打赏
  • 举报
回复
一个问题不懂什么意思, 第二问题没释放文件,在结尾加上


m_streamReader.Close();
m_streamReader.Dispose();
fs.Close();
fs.Dispose();
俺老刘 2012-08-08
  • 打赏
  • 举报
回复

感谢东方尚的提示,我根据你的思路做了个demo,现在有两个很严重的问题。
1.下拉列表不能再第一次的时自动选择第一项
2.第一次运行时可以,我多选几次就会发生错误,提示文本文件被另一个进程占用,不知道该如何解决。你能帮我修改一下代码吗?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace mytest
{
public partial class Form4 : Form

{
string strpath = @"C:\student.txt";//设定要打开的文件路径
string strLine;
int line = 0;
public Form4()
{
InitializeComponent();
}
//当下拉框中内容改变时
private void cmbStudent_TextChanged(object sender, EventArgs e)
{

try
{
FileStream fs = new FileStream(strpath, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
while ((strLine = sr.ReadLine()) != null && strLine.Length > 0)
{

string[] spli = strLine.Split('|');
if (cmbStudent.Text == spli[0])
{
txtName.Text = spli[0];
txtYuwen.Text = spli[1];
txtShuxue.Text = spli[2];
txtYingyu.Text = spli[3];

return;
}

line++;

}

sr.Dispose();
sr.Close();
fs.Dispose();
fs.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}

}
//窗体载入时
private void Form4_Load(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream(strpath, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
while ((strLine = sr.ReadLine()) != null && strLine.Length > 0)
{

string[] spli = strLine.Split('|');
cmbStudent.Items.Add(spli[0]);
//cmbStudent.Items.Insert(line, spli[0]);
//MessageBox.Show(strLine);
line++;

}
cmbStudent.SelectedIndex = 0;
sr.Dispose();
sr.Close();
fs.Dispose();
fs.Close();

}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}


}



}
}

dongfangshang2 2012-08-08
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int line = 0;
string strLine="";
FileStream fs;
OpenFileDialog openf = new OpenFileDialog();
openf.InitialDirectory = "c:\\";
openf.Filter = "exe files (*.txt)|*.txt";
openf.FilterIndex = 2;
openf.RestoreDirectory = true;
if (openf.ShowDialog() == DialogResult.OK)
{
try
{
fs = new FileStream(openf.FileName, FileMode.Open);
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
do
{
strLine = m_streamReader.ReadLine();
line++;

} while (strLine != null && strLine.Length > 0);

string[,] array = new string[line, 4]; // 声明输出数组
m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
line = 0;
do
{
strLine = m_streamReader.ReadLine();
if (strLine!=null && strLine.Contains('|'))
{
string[] spli = strLine.Split('|');
array[line, 0] = spli[0];
array[line, 1] = spli[1];
array[line, 2] = spli[2];
array[line, 3] = spli[3];
line++;
}

} while (strLine != null && strLine.Length > 0);

string temp = "";
for (int i = 0; i < line; i++)
{
temp = temp + array[i, 0];
temp = temp + "|" + array[i, 1];
temp = temp + "|" + array[i, 2];
temp = temp + "|" + array[i, 3] + "\n";
}
MessageBox.Show(temp);

}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}
}
}

winner2050 2012-08-07
  • 打赏
  • 举报
回复
string[,] data = new string[3,4];
string str =@"张三|80|90|85
李四|82|95|74
王五|79|80|95";

string[] arrX = str.Split('\r');
for (int x = 0; x < arrX.Length; x++)
{
string[] arrY = arrX[x].Split('|');
for (int y = 0; y < arrY.Length; y++)
{
data[x, y] = arrY[y];
}
}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧