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 appreci
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.IO;
namespace part_B_19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
StreamReader sr = new StreamReader(@"C:\Users\Acer\Documents\Visual Studio 2012\Projects\combobox.txt");
string line = sr.ReadLine();
while (line != null)
{
comboBox1.Items.Add(line);
line = sr.ReadLine();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
namespace CDKatalog
{
public partial class KorisnickoUputstvo : System.Web.UI.Page
{
string[] izvodjac = new string[20];
string[] nazivAlbuma = new string[20];
string[] zanr = new string[20];
string[] godinaIzdavanja = new string[20];
string[] izdavackaKuca = new string[20];
string[] slikaOmota = new string[20];
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1990; i <= 2019; i++)
{
DropDownList2.Items.Add(i.ToString());
}
StreamReader sr = File.OpenText(Server.MapPath(@"\textFajl\katalog.txt"));
string sadrzaj = sr.ReadToEnd();
int brojac = 1;
int j = 0;
for (int i = 0; i < sadrzaj.Length; i++)
{
if (sadrzaj[i] == '^') { j++; brojac++; }
else if (sadrzaj[i] == '|')
{
brojac++;
}
else if (brojac % 6 == 1)
{
izvodjac[j] = izvodjac[j] + sadrzaj[i];
}
else if (brojac % 6 == 2)
{
nazivAlbuma[j] = nazivAlbuma[j] + sadrzaj[i];
}
else if (brojac % 6 == 3)
{
zanr[j] = zanr[j] + sadrzaj[i];
}
else if (brojac % 6 == 4)
{
godinaIzdavanja[j] = godinaIzdavanja[j] + sadrzaj[i];
}
else if (brojac % 6 == 5)
{
izdavackaKuca[j] = izdavackaKuca[j] + sadrzaj[i];
}
else if (brojac % 6 == 0)
{
slikaOmota[j] = slikaOmota[j] + sadrzaj[i];
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Izvodjac", typeof(string));
dt.Columns.Add("Naziv Albuma", typeof(string));
dt.Columns.Add("Zanr", typeof(string));
dt.Columns.Add("Godina Izdavanja", typeof(string));
dt.Columns.Add("Izdavacka Kuca", typeof(string));
string pomoc = "";
for (int i = 1; i < 7; i++)//OVDE TREBA MENJATI BROJ
{
for (int c = 0; c < TextBox1.Text.Length; c++)
{
if (TextBox1.Text[c] == izvodjac[i][c + 2])
{
pomoc = pomoc + TextBox1.Text[c];
}
else {
pomoc = "";
break;
}
}
if (pomoc != "")
{
dt.Rows.Add(izvodjac[i], nazivAlbuma[i], zanr[i], godinaIzdavanja[i], izdavackaKuca[i]);
}
}
Label1.Text = nazivAlbuma[1][1].ToString();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Izvodjac", typeof(string));
dt.Columns.Add("Naziv Albuma", typeof(string));
dt.Columns.Add("Zanr", typeof(string));
dt.Columns.Add("Godina Izdavanja", typeof(string));
dt.Columns.Add("Izdavacka Kuca", typeof(string));
string pomoc = "";
for (int i = 1; i < 7; i++)
{
for (int c = 0; c < TextBox2.Text.Length; c++)
{
if (TextBox2.Text[c] == nazivAlbuma[i][c])
{
pomoc = pomoc + TextBox2.Text[c];
}
else
{
pomoc = "";
break;
}
}
if (pomoc != "")
dt.Rows.Add(izvodjac[i], nazivAlbuma[i], zanr[i], godinaIzdavanja[i], izdavackaKuca[i]);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button5_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Izvodjac", typeof(string));
dt.Columns.Add("Naziv Albuma", typeof(string));
dt.Columns.Add("Zanr", typeof(string));
dt.Columns.Add("GodinaIzdavanja", typeof(string));
dt.Columns.Add("Izdavacka Kuca", typeof(string));
string pomoc = "";
for (int i = 1; i < 7; i++)
{
for (int c = 0; c < TextBox3.Text.Length; c++)
{
if (TextBox3.Text[c] == izdavackaKuca[i][c])
{
pomoc = pomoc + TextBox3.Text[c];
}
else
{
pomoc = "";
break;
}
}
if (pomoc != "")
dt.Rows.Add(izvodjac[i], nazivAlbuma[i], zanr[i], godinaIzdavanja[i], izdavackaKuca[i]);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Izvodjac", typeof(string));
dt.Columns.Add("Naziv Albuma", typeof(string));
dt.Columns.Add("Zanr", typeof(string));
dt.Columns.Add("Godina Izdavanja", typeof(string));
dt.Columns.Add("Izdavacka Kuca", typeof(string));
for (int i = 0; i < 7; i++)
{
if (DropDownList1.SelectedValue == zanr[i]) {
dt.Rows.Add(izvodjac[i], nazivAlbuma[i], zanr[i], godinaIzdavanja[i], izdavackaKuca[i]);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button4_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Izvodjac", typeof(string));
dt.Columns.Add("NazivAlbuma", typeof(string));
dt.Columns.Add("Zanr", typeof(string));
dt.Columns.Add("Godina Izdavanja", typeof(string));
dt.Columns.Add("Izdavacka Kuca", typeof(string));
for (int i = 0; i < 7; i++)
{
if (DropDownList2.SelectedValue == godinaIzdavanja[i])
{
dt.Rows.Add(izvodjac[i], nazivAlbuma[i], zanr[i], godinaIzdavanja[i], izdavackaKuca[i]);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
It's just one line of code:
string content = System.IO.File.ReadAllText(@"C:\textfile.txt");
Check this code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
'openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
dataFile = openFileDialog1.FileName
Label1.Text = openFileDialog1.SafeFileName
Dim myReader As New StreamReader(dataFile)
Dim line As String
line = myReader.ReadLine()
While Not (line Is Nothing)
Dim str() As String = Split(line, ControlChars.Tab)
ListView1.Items.Add(New ListViewItem(str))
line = myReader.ReadLine()
End While
myReader.Close()
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
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);
Use Split() like in the code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"C:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string inputLine = "";
List<List<int>> data = new List<List<int>>();
while ((inputLine = reader.ReadLine()) != null)
{
string[] inputArray = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (inputArray.Count() > 0)
{
List<int> numbers = inputArray.Select(x => int.Parse(x)).ToList();
data.Add(numbers);
}
}
}
}
}