回复 10楼 voidx
我对C这几天用才看了二天,所以好多不太懂,有时间能帮我完善一下不,只需要对指定的第5列查找就行,只是数字,提示查3的行,如何实现?
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using namespace ReadTxt { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) { DialogResult d = openFileDialog1.ShowDialog(); if (d == DialogResult.OK) { txtPath.Text = openFileDialog1.FileName; } } private void btnSel_Click(object sender, EventArgs e) { DialogResult d = saveFileDialog1.ShowDialog(); if (d == DialogResult.OK) { txtSave.Text = saveFileDialog1.FileName; } } private void btnOK_Click(object sender, EventArgs e) { int col = 0; string num = ""; try { StreamReader sr = new StreamReader(txtPath.Text); //原始文件 StreamWriter sw = new StreamWriter(txtSave.Text); //保存在这里 col = int.Parse(txtCol.Text); num = txtText.Text; try { string[] str = new string[300]; //如果不超过300列 string s = ""; char[] ch ={ ';' }; //以分号分隔字符串,注意要用英文符号 while (sr.Peek() != -1) //检查文件结束 { s = sr.ReadLine(); //读行 str = s.Split(ch, StringSplitOptions.RemoveEmptyEntries); //分隔字符串,存入str中 if (str[col - 1].CompareTo(num) == 0) //相等 { sw.WriteLine(s); //写入文件 } } label4.Text = "运行成功"; } catch (Exception a) //程序异常处理 { label4.Text = "运行失败"; } finally //关闭文件流 { sr.Close(); sw.Close(); } } catch (Exception) { MessageBox.Show("输入有误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void Form1_Load(object sender, EventArgs e) { label4.Text = "准备就绪"; } } }