| | |
C# newbie. Read String and Parse.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
Hello,
I asked this question in the c++ forums but I've since moved the small program that I was working on over to C# Forms.
have a current file that I am opening. Each line of said file looks like this:
D40001~10997~811~DANIWEB~555-555-5555~7.70~I~2111
There are around 5000 lines with different DXXXXX numbers. I want the user to input a dnumber (DXXXXX) and have it search the file for said Dnumber and then display the name that is associated with the Dnumber. So typing in 'D40001' should display the name Daniweb. Here is my code but I've went through 5 tutorials on the net and cannot find out how to search this file and do what I want it to do. From the searching that I did do, they said that I need to parse the file and then match the DXXXXX and display the screen. I'm clueless to how to do this. Here is the code that I have so far. Also, this is not for homework. I'm too poor for school. :]
here is the code that I currently have.
I asked this question in the c++ forums but I've since moved the small program that I was working on over to C# Forms.
have a current file that I am opening. Each line of said file looks like this:
D40001~10997~811~DANIWEB~555-555-5555~7.70~I~2111
There are around 5000 lines with different DXXXXX numbers. I want the user to input a dnumber (DXXXXX) and have it search the file for said Dnumber and then display the name that is associated with the Dnumber. So typing in 'D40001' should display the name Daniweb. Here is my code but I've went through 5 tutorials on the net and cannot find out how to search this file and do what I want it to do. From the searching that I did do, they said that I need to parse the file and then match the DXXXXX and display the screen. I'm clueless to how to do this. Here is the code that I have so far. Also, this is not for homework. I'm too poor for school. :]
here is the code that I currently have.
namespace DealerNumberSearch { partial class Form1 { ///<summary> /// Required designer variable. ///</summary> private System.ComponentModel.IContainer components = null; ///<summary> /// Clean up any resources being used. ///</summary> ///<param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } public void OpenFile() { // Try to open the file try { file = new FileStream("dealers.tdl", FileMode.Open, FileAccess.Read); } // If file doesnt exist, throw up an error message. catch (Exception) { MessageBox.Show("The file could not be found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } public void SearchText() { stream = new StreamReader(file); line = stream.ReadLine(); } #region Windows Form Designer generated code ///<summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///</summary> private void InitializeComponent() { this.goButton = new System.Windows.Forms.Button(); this.getNumber = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // goButton // this.goButton.Location = new System.Drawing.Point(201, 40); this.goButton.Name = "goButton"; this.goButton.Size = new System.Drawing.Size(33, 20); this.goButton.TabIndex = 0; this.goButton.Text = "GO"; this.goButton.UseVisualStyleBackColor = true; this.goButton.Click += new System.EventHandler(this.goButton_Click); // // getNumber // this.getNumber.Location = new System.Drawing.Point(57, 40); this.getNumber.Name = "getNumber"; this.getNumber.Size = new System.Drawing.Size(100, 20); this.getNumber.TabIndex = 1; this.getNumber.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(122, 149); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(35, 13); this.label1.TabIndex = 3; this.label1.Text = "label1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.label1); this.Controls.Add(this.getNumber); this.Controls.Add(this.goButton); this.Name = "Form1"; this.Text = "DNS - Dealer Number Search"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private FileStream file; private StreamReader stream; private string line; private string val1; private System.Windows.Forms.Button goButton; private System.Windows.Forms.TextBox getNumber; private System.Windows.Forms.Label label1; } }
Last edited by chuck577; Nov 2nd, 2006 at 11:51 pm.
Heres a console aplication that basically does what you talking about. Its case sensitive, so when you are prompted for a number you would have to enter D40001 to get the name. This program basically enters the whole file into a arraylist and searches it. It could search your file using less memory (by not saving the information to the arraylist) with a few modifications.
c Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { private struct DXX { public string DXXNumber; public string DXXName; } static void Main(string[] args) { ArrayList a = new ArrayList(); System.IO.StreamReader sr = new System.IO.StreamReader("C:/Documents and Settings/Jeromy/My Documents/MCSD Self paced training kit/Projects/CodeTextBuilder/CodeTextBuilder/dealers.tdl"); while (!sr.EndOfStream) { string s = sr.ReadLine(); char[] splitchars = { '~' }; string[] sarr = s.Split(splitchars); DXX d = new DXX(); // This is assuming that your dxx number is always first and the name is always the 4th entry. d.DXXNumber = sarr[0]; d.DXXName = sarr[3]; a.Add(d); } sr.Close(); System.Console.WriteLine("Enter the DXX code for the name you want to retrieve." + " Enter 'done' if your finished searching"); string sin = System.Console.ReadLine(); string name = ""; bool found = false; while (!sin.Equals("done")) { foreach (DXX dl in a) { if (dl.DXXNumber.Equals(sin)) { found = true; name = dl.DXXName; break; } } if (found) { System.Console.WriteLine("Name: " + name); found = false; } else System.Console.WriteLine("That entry is not found."); System.Console.WriteLine("Enter the DXX code for the name you want to retrieve." + "Enter 'done' if your finished searching"); sin = System.Console.ReadLine(); } } } }
![]() |
Similar Threads
- Read a string from a binary file into a C or C++ string (C++)
- Read in a string from a text file (C)
- newbie needs help, basic mfc stuff (C++)
- Multiple data being read in (C++)
- why i have to press "enter" twice before getline can read the string... (C++)
- How do I read a line? (Java)
Other Threads in the C# Forum
- Previous Thread: axWebBrowser: DocumentComplete
- Next Thread: P2P based chat
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml





