Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading From a Text File in C#

Tags:

c#

c#-4.0

I have the following program that will send (output) information to a text file, but now I want to read (input) from the text file. Any suggestions would be greatly appreciated. I have commented out a couple of things that "I think" I need to do; but I am not really certain how to proceed.

using System.Windows.Forms; using System.IO;  namespace Input_Output {     public partial class Grades : Form     {         private StreamWriter output;          private StreamReader input;           public Grades()         {             InitializeComponent();         }          private void label4_Click(object sender, EventArgs e)         {          }          private void btnCreate_Click(object sender, EventArgs e)         {             btnEnter.Visible = true;             btnClose.Visible = true;             txtFirst.Visible = true;             txtLast.Visible = true;             lblFirst.Visible = true;             lblLast.Visible = true;             listBox1.Visible = true;             lblStatus.Visible = true;             lblGrade.Visible = true;             lblCourse.Visible = true;             cmbID.Visible = true;             lblID.Visible = true;             txtCourse.Visible = true;             txtGrade.Visible = true;             lblStatus.Visible = true;              DialogResult result;             string fileName;             using (SaveFileDialog chooser = new SaveFileDialog())             {                 result = chooser.ShowDialog();                 fileName = chooser.FileName;             }             output = new StreamWriter(fileName);             btnCreate.Enabled = false;             txtFirst.Visible = true;             txtLast.Visible = true;             lblFirst.Visible = true;             lblLast.Visible = true;             btnEnter.Visible = true;             btnClose.Visible = true;         }           private void btnClose_Click(object sender, EventArgs e)         {             //Close button pushes information from the listbox in to the text file              output.Close();             lblStatus.ForeColor = Color.Red;             lblStatus.Text = "Output File";             btnCreate.Enabled = true;             listBox1.Items.Clear();             cmbID.Text = "";         }          private void btnEnter_Click(object sender, EventArgs e)         {             // Enter button sends information to the list box, a Message Box prompts user to check for accuracy.               //Close button pushes information to the Text file.             string last = "";             string first = "";             string course = "";             string grade = "";              if (txtFirst.Text != "" && txtLast.Text != "" && txtCourse.Text != "")             {                 last = txtFirst.Text;                 first = txtLast.Text;                 course = txtCourse.Text;                 grade = txtGrade.Text;                 output.WriteLine (last + "\t"+ "\t" + first + ":"+ "\t" + cmbID.SelectedItem + "_" + course + "_" + grade );                  listBox1.Items.Add(txtLast.Text + "," + txtFirst.Text + ":" + cmbID.SelectedItem + "-" + txtCourse.Text + "-" + txtGrade.Text);                 lblStatus.ForeColor = Color.Navy;                 lblStatus.Text = "Entry Saved";                 txtFirst.Text = "";                 txtLast.Text = "";                 txtCourse.Text = "";                 txtGrade.Text = "";                 txtFirst.Focus();             }             else             {                      lblStatus.ForeColor = Color.Red;                 lblStatus.Text = "Empty text box or boxes";             }             MessageBox.Show("Please verify that the information is correct before proceeding");         }          private void btnExit_Click(object sender, EventArgs e)         {             Application.Exit();         }          private void Grades_Load(object sender, EventArgs e)         {          }          private void button1_Click(object sender, EventArgs e)         {             DialogResult result;             string fileName;             using (OpenFileDialog chooser = new OpenFileDialog())             {                 result = chooser.ShowDialog();                 fileName = chooser.FileName;             }             //while loop?             //if variable is null, it's the end of the record             //variable= !null              //txt read int variable TxtFile.Text += Rec + "\r\n";   while rec !=null;         }     } } 
like image 995
Infiniti68 Avatar asked Nov 02 '11 12:11

Infiniti68


People also ask

How do you read and write from a file in C?

For reading and writing to a text file, we use the functions fprintf() and fscanf(). They are just the file versions of printf() and scanf() . The only difference is that fprintf() and fscanf() expects a pointer to the structure FILE.

What is a text file in C?

Text files in C are straightforward and easy to understand. All text file functions and types in C come from the stdio library. When you need text I/O in a C program, and you need only one source for input information and one sink for output information, you can rely on stdin (standard in) and stdout (standard out).

How do you take input from a file?

In order to read information from a file, or to write information to a file, your program must take the following actions. 1) Create a variable to represent the file. 2) Open the file and store this "file" with the file variable. 3) Use the fprintf or fscanf functions to write/read from the file.

How does read function work in C?

The read() function reads data previously written to a file. If any portion of a regular file prior to the end-of-file has not been written, read() shall return bytes with value 0. For example, lseek() allows the file offset to be set beyond the end of existing data in the file.


1 Answers

To read a text file one line at a time you can do like this:

using System.IO;  using (var reader = new StreamReader(fileName)) {     string line;     while ((line = reader.ReadLine()) != null)     {         // Do stuff with your line here, it will be called for each          // line of text in your file.     } } 

There are other ways as well. For example, if the file isn't too big and you just want everything read to a single string, you can use File.ReadAllText()

myTextBox.Text = File.ReadAllText(fileName); 
like image 116
Isak Savo Avatar answered Sep 22 '22 04:09

Isak Savo