Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

xin code ghi dữ liệu,sửa dữ liệu và xóa dữ liệu trên tệp text trong c# console application

23:38 09-05-2017 3.218 lượt xem 7 bình luận 14:23 13-05-2017

mình có bảng hiển thị thông tin đã được ghi và tệp text.bây giờ mình muốn sửa thông tin của  tên món"toan"  và xóa thông tin đó thì làm như nào ạ??

bảng hiển thị:

 

   STT   TÊN MÓN       SỐ LƯỢNG         GIÁ

       1                 toan                           13                        144
       2                  tu                             134                      144

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 00:02 10-05-2017

1. Đọc từng dòng sửa rồi ghi lại

2. Đọc cả file tìm tới chỗ đó mà replace rồi ghi đè

3. Seek

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 23:59 11-05-2017

k có sửa code dùm đâu nhé. chỉ cách để làm rồi bạn

Nguyễn Xuân Toản đã bình luận 21:27 11-05-2017

.THỰC SỰ LÀ MÌNH KO BIẾT GÌ VỀ PHẦN TỆP NÀY NHIỀU.TRÊN LỚP CÔ CHỈ DẠY PHẦN GHI VỚI ĐỌC NÊN MÌNH CHỈ LÀM ĐƯỢC NHIÊU ĐÓ.MAI MÌNH PHẢI NỘP BÀI TẬP NÀY ZỒI.MONG BẠN GIÚP MÌNH VỚI Ạ!MÌNH CẢM ƠN!!

Nguyễn Xuân Toản đã bình luận 21:21 11-05-2017

Đây là chương trình quản lí thực đơn cho một nhà hàng:đề bài yêu cầu như sau:

 

-        Quản lý được danh sách các món ăn bằng tệp text

-        Tra cứu và chọn được các món ăn

-        Tính được hóa đơn cho khác hàng

Chương trình cho phép đăng nhập với tài khoản admin và tài khoản người dùng. Với tài khoản admin có thể thêm, sửa, xóa các món ăn, còn tài khoản người dùng thì chỉ xem và tra cứu

mình làm dc phần thêm zồi nhưng phần xóa với phần sửa mình làm thì nó báo sai.

BẠN XEM QUA MÌNH SAI CHỖ NÀO VÀ CÓ THỂ SỬA GIÚP MÌNH ĐƯỢC KHÔNG Ạ?

Nguyễn Xuân Toản đã bình luận 21:17 11-05-2017

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace baitaplon_2222
{
    class Program
    {
        struct thucdon
        {
            public string tenmon;
            public int soluong;
            public double gia;
        }
        static thucdon[] list;
        static int n;
        static void input()
        {
            do
            {
                Console.WriteLine("[---------------NHẬP THÔNG TIN---------------]");
                    Console.Write("                Nhập số món ăn:");
                n = int.Parse(Console.ReadLine());
                if (n <= 0)
                Console.WriteLine("          Nhập sai!Yêu cầu nhập lại!          \n");
            } while (n <= 0);
            list = new thucdon[n];
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("          Nhập thông tin món ăn thứ {0}:          ", i + 1);
                    Console.Write("                    Tên món:");
                list[i].tenmon = Console.ReadLine();
                    Console.Write("                   Số lượng:");
                list[i].soluong = int.Parse(Console.ReadLine());

                    Console.Write("                      Gía:");
                list[i].gia = double.Parse(Console.ReadLine());
            }
            //**********************GHI TỆP***********************//
            StreamWriter thucdon = File.CreateText("D:/thucdon.txt");
              Console.WriteLine("[------------DANH SÁCH THỰC ĐƠN------------]\n");
              thucdon.WriteLine("       STT   TÊN MÓN   SỐ LƯỢNG   GIÁ       \n");
            for (int j = 0; j < list.Length; j++)
            { thucdon.WriteLine("       {0}   \t{1}   \t{2}   \t{3}", j + 1, list[j].tenmon, list[j].soluong, list[j].gia); }
            thucdon.Close();

        }
        static void output()
        {
            string[] toan = File.ReadAllLines("D:/thucdon.txt");
            foreach (string s in toan)
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
        }
        static void hoadon()
        {
            int a, m;
            string x;
            
                Console.WriteLine("[------------------HÓA ĐƠN-----------------]");
                Console.WriteLine("        Nhập số lượng món ăn đã dùng:");
            a = int.Parse(Console.ReadLine());
            double[] b = new double[a];
            for (int i = 0; i < a; i++)
            {
                    Console.Write("        Nhập món ăn thứ {0} đã dùng:", i + 1);
                x = Console.ReadLine();
                    Console.Write("               Nhập số lượng:");
                m = int.Parse(Console.ReadLine());
                b[i] = list[i].gia * m;
            }
            double s = 0;
            for (int i = 0; i < a; i++)
            {
                s = s + b[i];
            }
                Console.WriteLine("      Tổng tiền cần thanh toán là:{0}", s);
                Console.WriteLine("[-------------------------------------------]");

        }


        static void search()
        {
            StreamReader thucdon = File.OpenText("D:/thucdon.txt");
            string toan = thucdon.ReadLine();
            
            Console.WriteLine("[-----------------TÌM KIẾM-----------------]");
                Console.Write("          Nhập tên món ăn cần tìm:");
            toan = Console.ReadLine();
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i].tenmon.Contains(toan))
                {
            Console.WriteLine("            STT   TÊN MÓN  GIÁ\n");
                Console.Write("            {0} \t{1}  \t{2}", i + 1, list[i].tenmon, list[i].gia);
                }
                else
                {
            Console.WriteLine("       không có tên món ăn cần tìm!");
                }

            }

        }
        static void Add()//thêm
        {
            int a;
            StreamWriter thucdon1 = File.AppendText("D:/thucdon.txt");
            Console.Write("Nhập số lượng món ăn cần thêm :");
            a = int.Parse(Console.ReadLine());

            for (int i = 0;i<a ; i++)
            {
               
                Console.WriteLine("[-------------------THÊM-------------------]");
                Console.WriteLine("        Nhập thông tin món ăn thứ {0}:", i + 1);
                    Console.Write("                  Tên món:");
                list[i].tenmon = Console.ReadLine();
                    Console.Write("                 Số lượng:");
                list[i].soluong = int.Parse(Console.ReadLine());

                    Console.Write("                    Gía:");
                list[i].gia = double.Parse(Console.ReadLine());
                thucdon1.WriteLine("         {0}   \t{1}   \t{2}   \t{3}",++n, list[i].tenmon, list[i].soluong, list[i].gia);
            }
            thucdon1.Close();
        }

        static void xoa()//XÓA
        {
                string line, ten;
                StreamReader x = File.OpenText("E:/thucdon.txt");
                StreamWriter y = File.CreateText("E:/trunggian.txt");
                Console.Write("Nhap ten mon muon xoa:");
                ten = Console.ReadLine();
                line = x.ReadLine();
                while (line != null)
                {
                    if (list[0].tenmon.Contains(ten))
                        y.WriteLine(line);
                    line = x.ReadLine();
                }
                x.Close();
                y.Close();
                StreamWriter h = File.CreateText("E:/thucdon.txt");
                StreamReader k = File.OpenText("E:/trunggian.txt");
                line = k.ReadLine();
                while (line != null)
                {
                    h.WriteLine(line);
                    line = k.ReadLine();
                }
                h.Close();
                k.Close();
            }
        
        static void sua()//SỬA
        {//saiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
            string ten;
            Console.Write("Nhập tên món ăn cần sửa:");
            ten = Console.ReadLine();
            StreamReader thucdon = File.OpenText("D:/thucdon.txt");
            StreamWriter trunggian = File.CreateText("D:/trunggian2.txt");
            string line;
            while ((line = thucdon.ReadLine()) != null)
                trunggian.WriteLine(line);
            thucdon.Close();
            trunggian.Close();
            StreamReader trunggian2 = File.OpenText("D:/trunggian2.txt");
            string xau =trunggian2.ReadLine();

        }
        static int menu_admin()
        {
            Console.WriteLine("[-------------------MENU-------------------]");
            Console.WriteLine("|              1. Hiển thị                 |");
            Console.WriteLine("|              2. Hóa đơn                  |");
            Console.WriteLine("|              3. Tìm kiếm                 |");
            Console.WriteLine("|              4. Thêm                     |");
            Console.WriteLine("|              5. Sửa                      |");
            Console.WriteLine("|              6. Xóa                      |");
            Console.WriteLine("|              7. Thoát                    |");
            Console.WriteLine("|       Mời bạn chọn công việc (1-7)       |");
            Console.WriteLine("[------------------------------------------]");
            
            int toannguyen;
            toannguyen = int.Parse(Console.ReadLine());
            return toannguyen;
        }
        static int menu_khach()
        {
            Console.WriteLine("[-------------------MENU-------------------]");
            Console.WriteLine("|              1. Hiển thị                 |");
            Console.WriteLine("|              2. Tìm kiếm                 |");
            Console.WriteLine("|              3. Thoát                    |");
            Console.WriteLine("|       Mời bạn chọn công việc (1-3)       |");
            Console.WriteLine("[------------------------------------------]");

            int toan;
            toan = int.Parse(Console.ReadLine());
            return toan;
        }
            static void pass()
        {
            string usename, password;
            do
            {
                Console.Write("Nhập usename:");
                usename = Console.ReadLine();
                Console.Write("Nhập password:");
                password = Console.ReadLine();
                if ((usename != "khach" || password != "12345678") && (usename != "admin" || password != "quantri"))

                    Console.Write("Tài khoản hoặc mật khẩu không đúng.xin nhập lại!\n");
            } while ((usename != "khach" || password != "12345678") && (usename != "admin" || password != "quantri"));
            if (usename == "khach" || password == "12345678")
            
                {
                    do
                    {
                        Console.Clear();
                        switch (menu_khach())
                        {
                            case 1: output(); Console.ReadKey(); break;
                            case 2:search(); Console.ReadKey(); break;
                        }

                    } while (true);
                }
            else
            {
                if (usename == "admin" || password == "quantri")

                {do
                    {
                        Console.Clear();
                        switch(menu_admin())
                        {
                            case 1: output(); Console.ReadKey(); break;
                            case 2: hoadon(); Console.ReadKey(); break;
                            case 3: search(); Console.ReadKey(); break;
                            case 4: Add(); Console.ReadKey(); break;
                            case 5: sua(); Console.ReadKey(); break;
                            case 6: xoa(); Console.ReadKey(); break;
                            case 7: Environment.Exit(0); break;
                        }

                    } while (true);
                }
            }

        }
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            input();
            pass();
            Console.ReadKey();

           
            }
        }
    }
 

Câu hỏi mới nhất