这个程序有什么问题? [英] what is this programs problems?

查看:83
本文介绍了这个程序有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此程序从用户处获取一些信息并将其保存在文本文件中,但是当我们想要查看所有已保存的记录时,最后一个显示为





this program get some info from user and save it in a text file but when we want to see all saved records the last on just shown


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.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace repair
{

   

    public partial class Form1 : Form
    {
        private FileStream output;
        private FileStream input;
        private BinaryFormatter formtter = new BinaryFormatter();
        BinaryFormatter reader = new BinaryFormatter();
        public Form1()
        {
            InitializeComponent();
        }
        private void openfile()
        {
            output = new FileStream("C:\\Users\\lenovo\\Documents\\kar.txt",FileMode.Append,FileAccess.Write);
        }
        public void cleartextbox()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openfile();
            user User=new user();
            try
            {
                User.Name = textBox1.Text;
                User.Family = textBox2.Text;
                User.Job = textBox3.Text;
            }
            catch(FormatException)
            {
                MessageBox.Show("invaid format");
                output.Close();
                return;
            }
            formtter.Serialize(output, User);
            output.Close();
            cleartextbox();
        }

        private void button2_Click(object sender, EventArgs e)
        {
try
{
    input = new FileStream("C:\\Users\\lenovo\\Documents\\kar.txt", FileMode.Open, FileAccess.Read);
    textBox4.Text = "users are:";
    while (true)
    {
        user User = (user)reader.Deserialize(input);
        string result = User.Name+"\t" + User.Family+"\t" + User.Job+"\t";
        textBox4.Text ="\r\n" +result;
    }
}
            catch(FileNotFoundException)
{
    MessageBox.Show("file noot exists");
    return;
}
            catch(SerializationException)
{
    input.Close();
}
        }
        
    }
}







这是用户类:








and this is the user class:


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

namespace repair
{
    [Serializable]
    class user
    {
        private string name;
        private string family;
        private string job;
        public user()
        {
        name=" ";
        family = " ";
        job = " ";


    }
        public user( string namevalue,string familyvalue,string jobvalue)
        {
            name = namevalue;
            family = familyvalue;
            job = jobvalue;
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
            public string Family
            {
                get
                {
                    return family;
                }
                set
                {
                    family=value;
                }

            }
        public string Job
        {
            get
            {
                return job;
            }
            set
            {
                job=value;
            }
        }
        }
    }

推荐答案

使用stringbuilder如下

use stringbuilder as below
StringBuilder builder = new StringBuilder();
builder.Append("users are:");
builder.AppendLine
while (true)
{
    user User = (user)reader.Deserialize(input);
    builder.AppendLine(User.Name+"\t" + User.Family+"\t" + User.Job+"\t");
}
textBox4.Text= builder.ToString();


这篇关于这个程序有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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