将CSV记录写入并保存到文件中 [英] writing and saving as CSV records to a file

查看:95
本文介绍了将CSV记录写入并保存到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试使用对象数组构建一个联系人管理器程序来存储数据。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我需要选择将文件保存并加载为文件的CSV记录,但我不确定如何进行此操作。



任何指导都将不胜感激。



Hi I am trying to build a contact managers program using an object array to store the data. I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. I need to have the option to save and load files as CSV records to a file but I am unsure how to go about doing this.

Any guidance would be appreciated.

static void Main(string[] args)
        {
            //The MAXPLAYERS constant is the physical table size
            const Int32 MAXCONTACTS = 23;

            //Declare the player tables

            Contact[] contacts = new Contact[MAXCONTACTS];

            //Keep track of the actual number of players (i.e. logical table size)
            Int32 contactCount = 0;
            Int32 number = 0;
            String lastName = " ";
            String phoneNumber = " "; 
            String emailAddress = " "; 

            String firstName = " "; ;

            Console.WriteLine("Contact List");
            // display the menu to the user
            Console.WriteLine("Enter option or M for menu:");
            //Main Driver
            char menuItem;
            menuItem = GetMenuItem();
            while (menuItem != 'X')
            {

                ProcessMenuItem(menuItem, firstName, lastName, phoneNumber, emailAddress, contacts, ref contactCount, MAXCONTACTS);
                menuItem = GetMenuItem();
               
            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = IOConsole.GetChar((Console.ReadLine()));

            while (menuItem != 'C'
                && menuItem != 'L' && menuItem != 'X' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                //menuItem = IOConsole.GetChar((Console.ReadLine()));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
           Console.WriteLine("C-> Create Contacts");
           Console.WriteLine("R-> Remove Contacts");
           Console.WriteLine("U-> Update Contacts");
           Console.WriteLine("D -> Load data from file");
           Console.WriteLine("S-> Save data to file");
           Console.WriteLine("L-> View sorted by last name");
           Console.WriteLine("F-> View sorted by first name");
           Console.WriteLine("P-> View by partial name search");
           Console.WriteLine("T-> View by contact type");
           Console.WriteLine("Q-> Quit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, String firstName, String lastName, String phoneNumber,
            String emailAddress, Contact[] contacts, ref Int32 contactCount, Int32 MAXCONTACTS)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    removeContact();
                    break;
                case 'U':
                    updateContact();
                    break;
                case 'D':
                    LoadToFile();
                    break;
                case 'S':
                    saveToFile();
                    break;
                    
                case 'L':
                    sortByLastName();
                    break;
                case 'F':
                    sortByFirstName();
                       break;
                case 'P':
                       
                       break;
                case 'T':
                       
                       break;
                case 'Q':
                       
                       break;

            }                   
        }


        public static void saveToFile()
        {

        }
        public static void LoadToFile()
        {

        }

推荐答案

快速CSV阅读器 [ ^ ]



CSV到表格到CSV [ ^ ]


因为CSV是完全已知的并且是简单的le text,尝试手工构建一个csv文件,看它应该是什么样子。



然后编写代码自动生成文件。



读取csv文件比较棘手。

你必须逐行读取它,然后将行拆分为字段并将字段存储在表格中。 />


您将通过尝试自己,滥用调试器来了解加载和保存期间附加的内容。
Since CSV is perfectly known and is simple text, try to build a csv file by hand to see what it should look like.

Then write the code to generate automatically the file.

Reading the csv file is more tricky.
you must read it line by line, then split the line to fields and store the fields in table.

You will learn a lot by trying yourself, and abuse of debugger to see what append during load ans save.


这篇关于将CSV记录写入并保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆