C# 从文件中读取数据

C# 从文件中读取数据,c#,file,text,C#,File,Text,如果您想下载应用程序,请点击以下链接: 我试图创建一个简单的银行应用程序,从文本文件中读取数据。到目前为止,我已经设法读入了所有的客户,其中有20位。然而,当读取账户和交易内容时,它只读取20个,但文本文件中还有很多 这是我到目前为止所拥有的。我认为这与getNextCustomer方法中的嵌套for循环有关 using System; using System.Collections; using System.Collections.Generic; using System.Compo

如果您想下载应用程序,请点击以下链接:

我试图创建一个简单的银行应用程序,从文本文件中读取数据。到目前为止,我已经设法读入了所有的客户,其中有20位。然而,当读取账户和交易内容时,它只读取20个,但文本文件中还有很多

这是我到目前为止所拥有的。我认为这与getNextCustomer方法中的嵌套for循环有关

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

namespace e_SOFT_Banking
{
    public partial class Form1 : Form
    {
        public static ArrayList bankDetails = new ArrayList();
        public static ArrayList accDetails = new ArrayList();
        public static ArrayList tranDetails = new ArrayList();

        string inputDataFile = @"C:\e-SOFT_v1.txt";

        const int numCustItems = 14;
        const int numAccItems = 7;
        const int numTransItems = 5;

        public Form1()
        {
            InitializeComponent();
            setUpBank();
        }

        private void btnShowData_Click_1(object sender, EventArgs e)
        {
            showListsOfCust();
        }

        private void setUpBank()
        {
            readData();
        }

        private void showListsOfCust()
        {
            listBox1.Items.Clear();

            foreach (Customer c in bankDetails)
                listBox1.Items.Add(c.getCustomerNumber() + " " + c.getCustomerTitle() + " " + c.getFirstName()
                                   + " " + c.getInitials() + " " + c.getSurname() + " " + c.getDateOfBirth()
                                   + " " + c.getHouseNameNumber() + " " + c.getStreetName() + " " + c.getArea()
                                   + " " + c.getCityTown() + " " + c.getCounty() + " " + c.getPostcode()
                                   + " " + c.getPassword() + " " + c.getNumberAccounts());

            foreach (Account a in accDetails)
                listBox1.Items.Add(a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
                                   + " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());

            foreach (Transaction t in tranDetails)
                listBox1.Items.Add(t.getDate() + " " + t.getType() + " " + t.getDescription() + " " + t.getAmount()
                                   + " " + t.getBalAfter());
        }

        private void readData()
        {
            StreamReader readerIn = null;
            Transaction curTrans;
            Account curAcc;
            Customer curCust;
            bool anyMoreData;
            string[] customerData = new string[numCustItems];
            string[] accountData = new string[numAccItems];
            string[] transactionData = new string[numTransItems];

            if (readOK(inputDataFile, ref readerIn))
            {
                anyMoreData = getNextCustomer(readerIn, customerData, accountData, transactionData);

                while (anyMoreData == true)
                {
                    curCust = new Customer(customerData[0], customerData[1], customerData[2], customerData[3], customerData[4],
                                           customerData[5], customerData[6], customerData[7], customerData[8], customerData[9],
                                           customerData[10], customerData[11], customerData[12], customerData[13]);

                    curAcc = new Account(accountData[0], accountData[1], accountData[2], accountData[3], accountData[4],
                                         accountData[5], accountData[6]);

                    curTrans = new Transaction(transactionData[0], transactionData[1], transactionData[2], transactionData[3], 
                                               transactionData[4]);

                    bankDetails.Add(curCust);
                    accDetails.Add(curAcc);
                    tranDetails.Add(curTrans);

                    anyMoreData = getNextCustomer(readerIn, customerData, accountData, transactionData);
                }

                if (readerIn != null)
                    readerIn.Close();
            }
        }

        private bool getNextCustomer(StreamReader inNext, string[] nextCustomerData, string[] nextAccountData, string[] nextTransactionData)
        {
            string nextLine;
            int numCItems = nextCustomerData.Count();
            int numAItems = nextAccountData.Count();
            int numTItems = nextTransactionData.Count();

            for (int i = 0; i < numCItems; i++)
            {
                nextLine = inNext.ReadLine();
                if (nextLine != null)
                {
                    nextCustomerData[i] = nextLine;
                    if (i == 13)
                    {
                        int cItems = Convert.ToInt32(nextCustomerData[13]);
                        for (int q = 0; q < cItems; q++)
                        {
                            for (int a = 0; a < numAItems; a++)
                            {
                                nextLine = inNext.ReadLine();
                                nextAccountData[a] = nextLine;
                                if (a == 6)
                                {
                                    int aItems = Convert.ToInt32(nextAccountData[6]);
                                    for (int w = 0; w < aItems; w++)
                                    {
                                        for (int t = 0; t < numTItems; t++)
                                        {
                                            nextLine = inNext.ReadLine();
                                            nextTransactionData[t] = nextLine;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                    return false;
            }
            return true;
        }

        private bool readOK(string readFile, ref StreamReader readerIn)
        {
            try
            {
                readerIn = new StreamReader(readFile);
                return true;
            }

            catch (FileNotFoundException notFound)
            {
                MessageBox.Show("ERROR Opening file (when reading data in)" + " - File could not be found.\n" + notFound.Message);
                return false;
            }

            catch (Exception e)
            {
                MessageBox.Show("ERROR Opening File (when reading data in)" + "- Operation failed.\n" + e.Message);
                return false;
            }
        }
    }
}
账户:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace e_SOFT_Banking
{
    class Account
    {
        private string accSort;
        private Int64 accNumber;
        private string accNick;
        private string accDate;  //not required - defaults to null
        private double accCurBal;
        private double accOverDraft;
        private int accNumTrans;

        public Account(string theAccSort, string theAccNumber, string theAccNick, 
                       string theAccDate, string theAccCurBal, string theAccOverDraft, 
                       string theAccNumTrans)
        {
            accSort = theAccSort;
            setAccNumber(theAccNumber);
            accNick = theAccNick;
            accDate = theAccDate;
            setAccCurBal(theAccCurBal);
            setAccOverDraft(theAccOverDraft);
            setAccNumTrans(theAccNumTrans);
        }

        public string getAccSort()
        {
            return accSort;
        }

        public long getAccNumber()
        {
            return accNumber;
        }

        public string getAccNick()
        {
            return accNick;
        }

        public string getAccDate()
        {
            return accDate;
        }

        public double getAccCurBal()
        {
            return accCurBal;
        }

        public double getAccOverDraft()
        {
            return accOverDraft;
        }

        public int getAccNumTrans()
        {
            return accNumTrans;
        }


        public void setAccSort(string inAccSort)
        {
            accSort = inAccSort;
        }

        public void setAccNumber(string inAccNumber)
        {
            try
            {
                accNumber = Convert.ToInt64(inAccNumber);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

        public void setAccNick(string inAccNick)
        {
            accNick = inAccNick;
        }

        public void setAccDate(string inAccDate)
        {
            accDate = inAccDate;
        }

        public void setAccCurBal(string inAccCurBal)
        {
            try
            {
                accCurBal = Convert.ToDouble(inAccCurBal);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

        public void setAccOverDraft(string inAccOverDraft)
        {
            try
            {
                accOverDraft = Convert.ToDouble(inAccOverDraft);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

        public void setAccNumTrans(string inAccNumTrans)
        {
            try
            {
                accNumTrans = Convert.ToInt32(inAccNumTrans);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }
    }
}
交易:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace e_SOFT_Banking
{
    class Transaction
    {
        private string date;
        private string type;
        private string description;
        private double amount;  //not required - defaults to null
        private double balAfter;


        public Transaction(string theDate, string theType, string theDescription,
                       string theAmount, string theBalAfter)
        {
            date = theDate;
            type = theType;
            description = theDescription;
            setAmount(theAmount);
            setBalAfter(theBalAfter);
        }

        public string getDate()
        {
            return date;
        }

        public string getType()
        {
            return type;
        }

        public string getDescription()
        {
            return description;
        }

        public double getAmount()
        {
            return amount;
        }

        public double getBalAfter()
        {
            return balAfter;
        }

        public void setDate(string inDate)
        {
            date = inDate;
        }

        public void setType(string inType)
        {
            type = inType;
        }

        public void setDescription(string inDescription)
        {
            description = inDescription;
        }

        public void setAmount(string inAmount)
        {
            try
            {
                amount = Convert.ToDouble(inAmount);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

        public void setBalAfter(string inBalAfter)
        {
            try
            {
                balAfter = Convert.ToDouble(inBalAfter);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }
    }
}

非常感谢您的帮助。

您的问题从以下几点开始

string[] customerData = new string[numCustItems];
string[] accountData = new string[numAccItems];
string[] transactionData = new string[numTransItems];
有了这样的结构和调用

anyMoreData = getNextCustomer(readerIn, customerData, accountData, transactionData);
对于一个客户,您将只获得一个accountData和一个transactionData


请重新设计您的代码,以便您的数据对象知道如何从数据流中读取自己。

问题:文件的结构是否可以更改,或者您是否必须按原样使用?你知道如何使用正则表达式吗?文本文件中实际有多少个帐户?什么是记录分隔符?不,不幸的是文件结构无法更改。不,我不知道如何使用正则表达式。账户、客户和交易的金额都是未知的,应该有所不同。至于delimeter,它是一行接一行的。据我所知,应用程序工作正常。60行将输出到列表框中,每个客户一行。这不是预期的吗?嗯,我知道它部分有效,因为有20个客户,但它只做20个帐户和交易。不过,还有很多。在查看输出后,我可以看到它正在收集所有客户,但只收集文本文件中的最后20个帐户和事务。调试它可以查看nextCustomerData.Count()中有多少记录问题可能不在循环本身,也可能没有获得客户数。或者打开文本文件,查看20条目,看看有什么不同,可能是字符不匹配
anyMoreData = getNextCustomer(readerIn, customerData, accountData, transactionData);