BinaryReader/Writer求和无输出,需开发TextMode存数求和C#窗体程序
嘿,我来帮你搞定这两个问题!
问题1:BinaryReader/BinaryWriter无输出无异常的排查与解决
你遇到的「无输出也无异常」大概率是因为流没有正确关闭或刷新——BinaryWriter会用缓冲区暂存数据,除非手动调用Flush()或者关闭流,否则数据不会真正写入文件。另外,读取时如果文件指针位置不对,也会出现读不到内容的情况。
给你几个关键修复点:
- 永远用
using语句包裹FileStream、BinaryWriter/BinaryReader,它会自动帮你释放资源并刷新数据,避免手动关闭遗漏的问题。 - 读取文件前,确保将文件指针重置到开头(用
Seek(0, SeekOrigin.Begin))。 - 处理文件不存在的情况,避免读取时报错。
举个完整的读写求和示例:
// 写入示例(向两个文件分别写入数字) using (FileStream fs = new FileStream(@"C:\Users\MYPC\Desktop\trial1.txt", FileMode.Create)) using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(25); // 写入整数25 } using (FileStream fs = new FileStream(@"C:\Users\MYPC\Desktop\trial2.txt", FileMode.Create)) using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(35); // 写入整数35 } // 读取两个文件并求和 int totalSum = 0; // 读取第一个文件 if (File.Exists(@"C:\Users\MYPC\Desktop\trial1.txt")) { using (FileStream fs = new FileStream(@"C:\Users\MYPC\Desktop\trial1.txt", FileMode.Open)) using (BinaryReader br = new BinaryReader(fs)) { totalSum += br.ReadInt32(); } } // 读取第二个文件 if (File.Exists(@"C:\Users\MYPC\Desktop\trial2.txt")) { using (FileStream fs = new FileStream(@"C:\Users\MYPC\Desktop\trial2.txt", FileMode.Open)) using (BinaryReader br = new BinaryReader(fs)) { totalSum += br.ReadInt32(); } } Console.WriteLine($"两个文件的数值总和:{totalSum}"); // 现在能正常输出结果了
问题2:WinForm数字保存与加法应用实现
根据你的需求,我帮你完善代码,实现「输入数字→点击Add→保存并显示总和」的功能,同时处理异常情况(比如输入非数字):
首先,假设你的Form上有:
- 一个
TextBox(命名为txtInput):用于输入要添加的数字 - 一个
Label(命名为lblResult):用于显示求和结果 - 一个
Button(命名为button1,Text设为「Add」):触发加法操作
完整代码如下:
namespace exam { public partial class Form1 : Form { // 定义文件路径,方便复用 private readonly string _filePath = @"C:\Users\MYPC\Desktop\trial.txt"; public Form1() { InitializeComponent(); // 初始化时加载并显示当前存储的数值 LoadCurrentTotal(); } // 加载文件中存储的当前总和 private void LoadCurrentTotal() { if (File.Exists(_filePath)) { using (FileStream fs = new FileStream(_filePath, FileMode.Open)) using (BinaryReader br = new BinaryReader(fs)) { lblResult.Text = $"当前总和:{br.ReadInt32()}"; } } else { // 文件不存在时初始化为0 lblResult.Text = "当前总和:0"; } } private void button1_Click(object sender, EventArgs e) { // 验证输入是否为有效整数 if (!int.TryParse(txtInput.Text, out int inputNumber)) { MessageBox.Show("请输入有效的整数!"); txtInput.Clear(); return; } // 获取当前存储的总和 int currentTotal = 0; if (File.Exists(_filePath)) { using (FileStream fs = new FileStream(_filePath, FileMode.Open)) using (BinaryReader br = new BinaryReader(fs)) { currentTotal = br.ReadInt32(); } } // 计算新总和 int newTotal = currentTotal + inputNumber; // 将新总和写入文件 using (FileStream fs = new FileStream(_filePath, FileMode.Create)) using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(newTotal); } // 更新显示并清空输入框 lblResult.Text = $"当前总和:{newTotal}"; txtInput.Clear(); } } }
额外说明:
- 用
int.TryParse处理输入,避免用户输入非数字时抛出异常 - 初始化时自动加载当前存储的数值,提升用户体验
- 所有流操作都用
using包裹,确保资源正确释放 - 如果需要用文本模式存储(而非二进制),只需把BinaryWriter/BinaryReader换成StreamWriter/StreamReader即可,示例:
// 文本模式写入 using (StreamWriter sw = new StreamWriter(_filePath)) { sw.WriteLine(newTotal.ToString()); } // 文本模式读取 using (StreamReader sr = new StreamReader(_filePath)) { currentTotal = int.Parse(sr.ReadLine()); }
内容的提问来源于stack exchange,提问作者PEEHU GANDHI
相关产品推荐
相关产品推荐

