如何在C#

时间:2017-12-11 00:00:26

标签: c# csv filestream streamreader

我正在为项目创建一个虚假的航班跟踪器,需要检查Flight.csv文件中的行总数(所有单个航班)是否与Airport.csv文件中的numOfFlights变量匹配。如何从两个文件中交叉检查这些数据,而不必将它们合并?

这是相关的代码,如果需要更多来解决问题,请使用lmk。

static void Main()
    {
        Flight[] flight = new Flight[1000];
        Airport[] airport = new Airport[1000];
        int flightCount = 0;
        int airportCount = 0;

        ReadFlightFile(ref flight, ref flightCount);
        ReadAirportFile(ref airport, ref airportCount);

        int numRecords = flightCount;            

        OutputFlightMenu();
        string userInput = "----";
        while (userInput != "exit")
        {
            userInput = ReadLine().ToLower();
            Clear();
            OutputFlightMenu();
            switch (userInput)
            {
                case "passengers":
                    //Stuff
                    break;
                case "time":
                    //Stuff
                    break;
                case "average":
                    //Stuff
                    break;
                case "sub":
                    OutputSubMenu();
                    string userInput2 = "----";
                    while (userInput2 != "quit")
                    {
                        userInput2 = ReadLine().ToLower();
                        Clear();
                        OutputSubMenu();
                        switch (userInput2)
                        {
                            case "flight":
                                //Stuff
                                break;
                            case "count":
                                //Stuff
                                break;
                            case "airline":
                                //Stuff
                                break;
                            default:
                                WriteLine("Invalid input. Please try again.");
                                break;

                        }
                    }
                    Clear();
                    WriteLine("====Exiting System===");
                    break;
                case "report":
                    //Stuff
                    break;
                default:
                    WriteLine("Invalid input. Please try again.");
                    break;
            }
        }
        Clear();
        WriteLine("=====Goodbye=====");
        ReadKey();

    }

    public static void ReadFlightFile(ref Flight[] flight, ref int flightCount)
    {
        string path = "Flight.csv";
        FileStream fs = new FileStream(path, FileMode.Open);
        StreamReader sr = new StreamReader(fs);
        while (sr.Peek() > -1)
        {
            string line = sr.ReadLine();
            string[] flightArray = line.Split(',');

            flight[flightCount] = new Flight(int.Parse(flightArray[0]), flightArray[1], int.Parse(flightArray[2]),
                int.Parse(flightArray[3]), int.Parse(flightArray[4]));

            flightCount++;

        }

    }
    //Basic Read Method
    public static void ReadAirportFile(ref Airport[] airport, ref int airportCount)
    {
        string path = "Airport.csv";
        FileStream fs = new FileStream(path, FileMode.Open);
        StreamReader sr = new StreamReader(fs);
        while (sr.Peek() > -1)
        {
            string line = sr.ReadLine();
            string[] airportArray = line.Split(',');

            airport[airportCount] = new Airport(int.Parse(airportArray[0]), airportArray[1], airportArray[2], 
                int.Parse(airportArray[3]), DateTime.Parse(airportArray[4]));

            airportCount++;
        }
    }

以下是各个文件的数据:

Flight.csv:

3446,德尔塔,91120120

2789,美国,34,56,78

9078,美国,200240210

9079,美国,67156165

4567,西南,89,89,91

6799,德尔塔,95,67,55

1265,美国,102187175

8421,美国,12191191

3332,西南,78,45,44

Airport.csv:

9000,ATL,亚特兰大,9,12 /二千〇一十七分之九

如您所见,机场阵列(9)中的numOfFlights元素与航班的总和相匹配。

对于这个项目的意图,我已经知道数据会匹配,但我需要能够验证它。

感谢任何能提供帮助的人。

3 个答案:

答案 0 :(得分:0)

airport.Where(o=>o.numOfFlights==flightCount)将返回numOfFlights等于flightCount的机场。您应该使用List而不是数组来存储Flight和Airport

答案 1 :(得分:0)

让Read方法接受文件名并返回List或数组(其中集合的大小为Count值)的更好样式:

public static IList<Flight> ReadFlightFile(string filePath)
{
    var result = new List<Flight>();
    foreach(var line in File.ReadLines(filePath))
    {
        string[] flightArray = line.Split(',');

        result.Add(new Flight(int.Parse(flightArray[0]), flightArray[1], int.Parse(flightArray[2]),
            int.Parse(flightArray[3]), int.Parse(flightArray[4]));

    }
    return result;
}

//Basic Read Method
public static IList<Airport> ReadAirportFile(string filePath)
{
    var result = new List<Airport>();
    foreach(string line in File.ReadLines(filePath))
    {
        string[] airportArray = line.Split(',');

        result.Add(new Airport(int.Parse(airportArray[0]), airportArray[1], airportArray[2], 
            int.Parse(airportArray[3]), DateTime.Parse(airportArray[4]));
    }
    return result;
}

这样做,您可以将其添加到其他代码中:

static void Main()
{
    var flights = ReadFlightFile("Flight.csv");
    var airport = ReadAirportFile("Airport.csv");   

    if (airport[0].numOfFlights == flights.Count)
    {
        //things match
    }
    else
    {
        //things don't match
    }       

    // rest of your code is down here:

}

答案 2 :(得分:0)

这比制作它简单得多。

要计算Flight.csv中的行数,您可以使用以下内容:

int lineCount = File.ReadAllLines("Your/Path/To/Flight.csv").Count();

如果你有标题你应该-1

要获得Airport.csv中的航班数量,只需将其打开,拆分为数组,然后抓住数字:

string[] AirportContents = File.ReadAllText("Your/Path/To/Airport.csv").Split(',');
int flightsToday = Convert.ToInt32(AirportContents[3]);

如果有标题,你可以将文件内容放入列表并阅读第二项[1](但你没有在你的例子中加上标题)

一旦你拥有了这两个数字,你可以随心所欲地做任何事情。