1

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

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

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 = 



                }
            }
4

2 回答 2

1

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

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

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

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

于 2014-12-22T04:21:01.767 回答
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
        }
    }
}
于 2014-12-22T04:45:07.607 回答