mấy bác góp ý dùng e
Vấn đề lưu tập tin
Em full code lên đi em. Không chụp ảnh nhé
KhachHang.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace bai1
{
class KhachHang
{
struct ThongTinA
{
public int stt;
public string TenKhachHang;
public int SoLuongHang;
public int DonGiaHang;
};
struct ThongTinB
{
public int stt;
public string TenKhachHang;
public int SoLuongHang;
public int DonGiaHang;
};
struct ThongTinC
{
public int stt;
public string TenKhachHang;
public int SoLuongHang;
public int DonGiaHang;
};
private ThongTinA TestThongTinA;
private ThongTinB TestThongTinB;
private ThongTinC TestThongTinC;
static FileStream F;
StreamWriter W;
StreamReader R;
public void NhapThongTin()
{
int chon = 0;
Console.Clear();
while (chon != 4)
{
Console.WriteLine("----------MENU-------------");
Console.WriteLine("1. Nhap khach hang loai A: ");
Console.WriteLine("2. Nhap khach hang loai B: ");
Console.WriteLine("3. Nhap khach hang loai C: ");
Console.WriteLine("4. EXIT!!!");
Console.WriteLine("---------------------------");
Console.Write("Chon: ");
chon = int.Parse(Console.ReadLine());
switch (chon)
{
case 1:
Console.Clear();
char ka;
do
{
Console.Write("Nhap stt: ");
TestThongTinA.stt = Convert.ToInt16(Console.ReadLine());
Console.Write("Nhap ten khach hang: ");
TestThongTinA.TenKhachHang = Console.ReadLine();
Console.Write("Nhap so luong hang: ");
TestThongTinA.SoLuongHang = Convert.ToInt32(Console.ReadLine());
Console.Write("Nhap don gia hang: ");
TestThongTinA.DonGiaHang = Convert.ToInt32(Console.ReadLine());
F = new FileStream("C:\\xyz.inp", FileMode.Append, FileAccess.Write);
W = new StreamWriter(F);
W.Write("Khach hang loai A");
W.Write("stt: "); W.Write(TestThongTinA.stt);
W.Write("Ten khach hang: "); W.Write(TestThongTinA.TenKhachHang);
W.Write("So luong hang: "); W.Write(TestThongTinA.SoLuongHang);
W.Write("Don gia hang: "); W.Write(TestThongTinA.DonGiaHang);
W.Write("close\n");
W.Flush();
W.Close();
Console.Write("Nhap tiep Khach hang loai A: (c/k) ");
ka = char.Parse(Console.ReadLine());
} while (ka == 'c');
break;
case 2:
Console.Clear();
char kb;
do
{
Console.Write("Nhap stt: ");
TestThongTinB.stt = Convert.ToInt16(Console.ReadLine());
Console.Write("Nhap ten khach hang: ");
TestThongTinB.TenKhachHang = Console.ReadLine();
Console.Write("Nhap so luong hang: ");
TestThongTinB.SoLuongHang = Convert.ToInt32(Console.ReadLine());
Console.Write("Nhap don gia hang: ");
TestThongTinB.DonGiaHang = Convert.ToInt32(Console.ReadLine());
F = new FileStream("C:\\xyz.inp", FileMode.Append, FileAccess.Write);
W = new StreamWriter(F);
W.Write("Khach hang loai B");
W.Write("stt: "); W.Write(TestThongTinB.stt);
W.Write("Ten khach hang: "); W.Write(TestThongTinB.TenKhachHang);
W.Write("So luong hang: "); W.Write(TestThongTinB.SoLuongHang);
W.Write("Don gia hang: "); W.Write(TestThongTinB.DonGiaHang);
W.Write("close\n");
W.Flush();
W.Close();
Console.Write("Nhap tiep Khach hang loai B: (c/k) ");
kb = char.Parse(Console.ReadLine());
} while (kb == 'c');
break;
case 3:
Console.Clear();
char kc;
do
{
Console.Write("Nhap stt: ");
TestThongTinC.stt = Convert.ToInt16(Console.ReadLine());
Console.Write("Nhap ten khach hang: ");
TestThongTinC.TenKhachHang = Console.ReadLine();
Console.Write("Nhap so luong hang: ");
TestThongTinC.SoLuongHang = Convert.ToInt32(Console.ReadLine());
Console.Write("Nhap don gia hang: ");
TestThongTinC.DonGiaHang = Convert.ToInt32(Console.ReadLine());
F = new FileStream("C:\\xyz.inp", FileMode.Append, FileAccess.Write);
W = new StreamWriter(F);
W.Write("Khach hang loai C");
W.Write("stt: "); W.Write(TestThongTinC.stt);
W.Write("Ten khach hang: "); W.Write(TestThongTinC.TenKhachHang);
W.Write("So luong hang: "); W.Write(TestThongTinC.SoLuongHang);
W.Write("Don gia hang: "); W.Write(TestThongTinC.DonGiaHang);
W.Write("close\n");
W.Flush();
W.Close();
Console.Write("Nhap tiep Khach hang loai B: (c/k) ");
kc = char.Parse(Console.ReadLine());
} while (kc == 'c');
break;
case 4: return;
default: Console.WriteLine("Chi nhap tu 1 toi 3."); break;
}
}
}
public void Xuat()
{
string str;
F = new FileStream("C://XYZ.INP", FileMode.Open, FileAccess.Read);
R = new StreamReader(F);
Console.Clear();
int Pos = 0;
while ((str = R.ReadLine()) != null)
{
while (true)
{
Pos = str.IndexOf(";");
if (Pos == -1) break;
Console.WriteLine(str.Substring(0, Pos));
str = str.Substring(Pos + 1);
}
Pos = 0;
}
R.Close();
}
}
}
program.cs
using System;
namespace bai1
{
class Program
{
static void Main(string[] args)
{
KhachHang KH = new KhachHang();
int ch = 0;
Console.Clear();
while (ch != 3)
{
Console.WriteLine("-------MENU------");
Console.WriteLine("1. Nhap Thong Tin");
Console.WriteLine("2. Xuat Thong Tin");
Console.WriteLine("3. EXIT!!!");
Console.WriteLine("-----------------");
Console.Write("Chon: ");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1:
Console.Clear();
char kn;
do
{
KH.NhapThongTin();
Console.Write("Tro ve: (c/k) ");
kn = char.Parse(Console.ReadLine());
} while (kn == 'c');
break;
case 2:
Console.Clear();
char kx;
do
{
KH.Xuat();
Console.Write("Tro ve: (c/k) ");
kx = char.Parse(Console.ReadLine());
} while (kx == 'c');
break;
case 3: return;
default:
Console.WriteLine("Chi nhap tu 1 toi 3.");
break;
}
}
}
}
}
Có thể là không có quền, Bạn thử lưu vào thư mục khác xem!
e lưu ổ D thì được có cách nào cấp quyền nó lưu vào thư mục project ko ạ e newbie mới học C#
Ổ C chứa hệ điều hành nên tất nhiên là nó đuọc bảo vệ rồi. Khi bạn copy 1 foldẻ vào ổ C thường thì có hộp thoại hỏi, hay xóa folder C:/program cũng thế. @_@!.
Lưu file xuống thư mục chạy thì bạn chỉ cần bỏ phần đường dẫn đi.
VD: muốn lưu file text.txt xuống thư mục chạy thì path “text.txt”
2 Likes
Anh biết code đếm số dòng trong file text ko ạ
string[] lines = File.ReadAllLine(filepath);
int count=lines.Lenght;
1 Like

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?