c# - 在 C# 中读取和写入文本文件

标签 c# text

我正在编写一个程序,需要在一个文本文件中读取、写入和过滤数据到新文件。
该计划的主要目标是:

  • 让用户选择一个包含我已创建的数据的文本文件,
  • 使用子字符串选择要从文件中获取的字符,
  • 写入与文本文件中的数据相匹配的文件。

我对让程序写入一般文件以及从文本文件中抓取某些字符感到有点困惑。
如果有人能给我一些指示,那就太好了。
谢谢!

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.Diagnostics;
using System.IO;

namespace Project_4_osmvoe
{
    public partial class Form1 : Form
    {
        string ham;
        StreamReader pizza;
        StreamWriter burger;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                ham = openFileDialog1.FileName;
            }
            pizza = new StreamReader(ham);
            lblInputFile.Text = ham;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                ham = saveFileDialog1.FileName;
            }
            burger = new StreamWriter(ham);
            lblOutputFile.Text = ham;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string line;
            while ((line = pizza.ReadLine()) != null)
            {
                if (filter(line))
                    burger.WriteLine(line);
            }
            pizza.Close();
            burger.Close();
            MessageBox.Show("Output File Written");
        }

        private Boolean filter(string intext)
        {
            string gender = intext.Substring(0, 0);
            string state = intext.Substring(0, 0);
            if (((radioButtonFemale.Checked && gender.Equals("F")) ||
                 (RadioButtonMale.Checked && gender.Equals("M"))))
                return true;
            else
                return false;
        }
    }
}

最佳答案

上述评论中收到的有用建议的一部分。
(不要在事件之间保持流打开)

你认为这些行的结果是什么?

    string gender = intext.Substring(0, 0);
    string state = intext.Substring(0, 0);

Substring 的第二个参数是要从字符串中提取的字符数。传递零意味着您返回的字符串为空,因此后续测试始终为 false 并且您永远不会写入一行。

我建议将两个文件的名称存储在两个不同的全局变量中,并在button3_Click中打开两个流

string inputFile;
string outputFile;

private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        inputFile = openFileDialog1.FileName;
        lblInputFile.Text = inputFile;
    }
}

private void button2_Click(object sender, EventArgs e)
{
    DialogResult result = saveFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        outputFile = saveFileDialog1.FileName;
        lblOutputFile.Text = outputFile ;
    }
}

private void button3_Click(object sender, EventArgs e)
{
    string line;
    using(StreamReader pizza = new StreamReader(inputFile))
    using(StreamWriter burger = new StreamWrite(outputFile))
    {
        while ((line = pizza.ReadLine()) != null)
        {
           if (!string.IsNullOrWhiteSpace(line) && filter(line))
               burger.WriteLine(line);
        }
    }
    MessageBox.Show("Output File Written");
}

private Boolean filter(string intext)
{
    string gender = intext.Substring(0, 1);
    string state = intext.Substring(0, 1);
    if (((radioButtonFemale.Checked && gender.Equals("F")) ||
         (RadioButtonMale.Checked && gender.Equals("M"))))
         return true;
    else
         return false;
}

关于c# - 在 C# 中读取和写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974288/

相关文章:

c# - 在 Entity Framework 中选择前 5 名

c# - 带菜单的按钮

java - 返回到 switch 中的过去的情况

c - 使用 strcpy 时打印垃圾

c# - 运行已编译的 C# 程序 (.exe) 的要求

c# - C#中如何取回子字符串的值

text - 文本的第二行不在行的中心呈现

python - Numpy savetxt 不能正确处理 18 ditgits long int

text - 关注 VStack 中的下一个 TextField

c# - 如何检查MediaElement是否已停止?