从文本文件中读取多行并将其保存到数组中的问题c#

时间:2014-12-22 02:59:06

标签: c# arrays forms system.io.file

所以我想要做的是读取一个文件,其中有一个数据段,如下所示。到目前为止,该程序从下拉菜单打开文件,但我很难将它们保存到数组中。我希望能够在打开文件(在文本文件中将最后三行从文本框打印到文本框中)后单击下一个按钮,并将下面文本文件示例中的每个信息行打印到一个单独的文本框。这就是我遇到问题的地方。

将姓名和地址保存到EmpNames类,然后将下面的.split()数字保存到各自的Employee Class中,以便设置为一系列计算,然后打印导致文本框。

1
John MerryWeather
123 West Main Street
5.00 30

会有多个这样的数据段,但不会超过10.这是我到目前为止所做的。

public partial class Form1 : Form
{
    const int MAX = 10;

    public Form1()
    {
        InitializeComponent();
    }


    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog theDialog = new OpenFileDialog();
        theDialog.Title = "Open Text File";
        theDialog.Filter = "TXT files|*.txt";
        theDialog.InitialDirectory = @"C:\";
        if (theDialog.ShowDialog() == DialogResult.OK)
        {
            //Declarations:
            //      linesPerEmployee: Controls the number of lines to be read.
            //      currEmployeeLine: Controls where in the file you are reading.

            Employee employee = new Employee();
            NameAdd empNames = new NameAdd();
            string filename = theDialog.FileName;



            List<Employee> employeeList = new List<Employee>();
            int linesPerEmployee = 4;
            int currEmployeeLine = 0;
            //parse line by line into instance of employee class


            while (employeeList != null)
            {
                string[] filelines = File.ReadAllLines(filename);
                if (filelines != null)
                {

                    employee.EmpNum = int.Parse(filelines[0]);
                    empNames.Name = 



                }
            }

2 个答案:

答案 0 :(得分:1)

不是读取一个块中的所有行,而是可以逐行读取它们并将每行添加到List<string>中,以便更轻松地处理&#34;行&#34;

var employees = new List<string>();
Stream file = theDialog.File.OpenRead();
while((line = file.ReadLine()) != null)
{
    employees.Add(line);
}

然后遍历员工列表,将每4行解析为Employee()

不过,我同意有关使用更好格式的评论。

答案 1 :(得分:0)

也同意其他人关于更好的文件格式,但是,如果您的数据没有出现重击,丢失或额外的行,对员工之间的序列编号的任何确认,无法转换的错误数据会发生什么? ......所有这些以及更多对当前格式说不好的主意。

然而,正如所说的那样,以及你已经发生的事情,我会把它包起来像......

string[] filelines = File.ReadAllLines(filename);
if (filelines != null)
{
    if (filelines.Length % 4 == 0)
    {
        // which array element are we getting to at the start of each employee.
        int arrayBase = 0;
        for( int i=0; i < (int)(filelines.Length / 4); i++ )
        {
            arrayBase = i*4;
            employee.EmpNum = int.Parse(filelines[arrayBase]);
            empNames.Name = filelines[arrayBase + 1];
            empNames.Address = filelines[arrayBase + 2];
            string[] rateAndHours = filelines[arrayBase + 3].Split(' ');
            // you would still have to parse the rate and hours though.
            double justRate = double.Parse(rateAndHours[0]);
            int justHours = int.Parse(rateAndHours[1]);
            // obviously add your own try\catch confirmation on parsing issues
            // and ultimately store in your record entries
        }
    }
}