Tôi đang viết một chương trình cho trường nên tôi không hoàn toàn muốn ai đó sửa nó cho tôi, nhưng nếu ai đó có thể chỉ ra nơi họ thấy lỗi, điều đó thực sự hữu ích ! : DỨng dụng thăm dò ý kiến: StreamWriter không ghi vào tập tin
Chương trình được coi là một chuỗi số trong hộp văn bản, với người dùng nhấn enter sau mỗi mục nhập số nguyên, khi họ nhấn enter, số ghi vào tệp, sao cho mỗi số được tách riêng bằng dấu phẩy. Khi người dùng nhấn nút Xong, nút và hộp văn bản bị vô hiệu hóa, khi đó một nút mới và một hộp danh sách sẽ được bật. Người dùng có thể chọn nút kết quả để xem trong hộp danh sách tần số của các số được nhập vào hộp văn bản.
Tôi cảm thấy rằng hầu hết nó hoạt động đúng cách ngoại trừ việc nó không thực sự ghi vào tệp. Đây chỉ là năm đầu tiên của tôi vì vậy tôi khá mới ở đây, nhưng từ những thông tin tôi có tất cả có vẻ đúng.
Nếu ai đó có thể chỉ ra nơi tôi nên tìm kiếm một lỗi, một lần nữa, tôi thực sự đánh giá cao nó! Đây là mã của tôi:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace StudentPoll1
{
public partial class Form1 : Form
{
const string FILENAME = "numbers.txt";
FileStream file = new FileStream(FILENAME,
FileMode.Create, FileAccess.ReadWrite);
public Form1()
{
InitializeComponent();
btnResult.Enabled = false;
}
private void tbInput_KeyDown(object sender, KeyEventArgs e)
{
const char DELIM = ',';
int input;
const int MIN = 1;
const int MAX = 10;
int.TryParse(tbInput.Text, out input);
if (e.KeyCode == Keys.Enter)
{
try
{
if (input < MIN || input > MAX)
{
MessageBox.Show("Please enter an integer between 1 and 10");
}
else
{
StreamWriter writer = new StreamWriter(file);
writer.WriteLine(input + DELIM + " ");
}
}
catch (IOException)
{
MessageBox.Show("Error with input");
}
finally
{
tbInput.Clear();
}
}
}
private void btnDone_Click(object sender, EventArgs e)
{
file.Close();
btnDone.Enabled = false;
btnResult.Enabled = true;
lbOutput.SelectionMode = SelectionMode.None;
}
private void btnResult_Click(object sender, EventArgs e)
{
int[] ratings = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] results = new int[10];
int entry;
const char DELIM = ',';
FileStream fs = new FileStream(FILENAME,
FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
string record;
string[] fields;
record = reader.ReadLine();
while (record != null)
{
fields = record.Split(DELIM);
entry = Convert.ToInt32(fields[0]);
foreach (int x in ratings)
{
if (entry == ratings[x])
{
++results[x];
}
}
}
for (int num = 0; num < ratings.Length; ++num)
{
lbOutput.Items.Add(ratings[num] + " " + results[num]);
}
fs.Close();
reader.Close();
}
}
}
cố gắng thêm 'writer.Flush()' sau khi ghi vào luồng –
Ngoài ra 'writer.Close()' vì bạn không sử dụng khối 'using' để đóng luồng I/O để ngăn chặn' Tệp đã có trong use' vào cuộc gọi tiếp theo. – Franck
Bạn cũng có thể đặt AutoFlush = true. –