user3517134
user3517134

Reputation: 21

Reading and writing text files in C#

I am coding a program where I need to read, write, and filter data from one text file to a new one.
The main goal of this program is:

I am a little stuck on getting the program to write files in general as well as grabbing certain characters from a text file.
If anyone could give me some pointers that would be awesome.
Thanks!

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;
        }
    }
}

Upvotes: 2

Views: 272

Answers (1)

Steve
Steve

Reputation: 216353

A part from the useful advices received in the comments above.
(Don't keep streams opened between events)

What do you think is the result of these lines?

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

THe second parameter of Substring is the number of chars to extract from the string. Passing zero means that your returned string is empty, so the subsequent test is always false and you never write a line.

I suggest to store, in two different global variables, the names of the two files and, in button3_Click open the two streams

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;
}

Upvotes: 6

Related Questions