Click here to Skip to main content

C#

   
Home > Forums > C#

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page  Show 
  Refresh
GeneralRe: C# Error, I don't understand PinmemberLilli Alexis5:42 10 Oct '05  
GeneralRe: C# Error, I don't understand PinmemberDavid Stone5:45 10 Oct '05  
GeneralRe: C# Error, I don't understand PinmemberLilli Alexis5:52 10 Oct '05  
GeneralRe: C# Error, I don't understand PinmemberS. Senthil Kumar5:58 10 Oct '05  
GeneralRe: C# Error, I don't understand PinmemberDavid Stone6:02 10 Oct '05  
QuestionData Mining using C# Pinmemberalvinjv3:06 10 Oct '05  
AnswerRe: Data Mining using C# PinmemberXRaheemX4:51 10 Oct '05  
Since the file is plain text you can use a simple text reader to read the file. Lets say you wanted to parse out all of the lines given into some respective parameters. You would first create a class file that can hold of of the attributes: (I'm writing all of this in the browser, not in a C# compiler so there may be some syntax mistakes...)
 
public class MyFile
{
   private string artist;
   private string album;
   private string year;
   private string song;
 
   public MyFile()
   {
   }
 
   public string Artist
   {  
      get
      {
          return this.artist;
      }
      set
      {
          this.artist = value;
      }
   }
 
   public string Album
   {  
      get
      {
          return this.album;
      }
      set
      {
          this.album = value;
      }
   }
 
   public string Year
   {  
      get
      {
          return this.year;
      }
      set
      {
          this.year = value;
      }
   }
 
   public string Song
   {  
      get
      {
          return this.song;
      }
      set
      {
          this.song = value;
      }
   }
}
 
Once you have this class you can read the file using a TextReader:
 
   System.IO.TextReader reader = new System.IO.TextReader(Application.StartupPath + "\\myfile.txt");
   try
   {
      MyFile customClass = new MyFile(); //Create an instance of the custom class above

      while(true)
      {
         string line = reader.ReadLine();
         if(line == null) //End of file
         {
             break;
         }
         ParseText(line,ref customClass);
      }
   }
   finally
   {
      if(reader != null)
      {
         reader.Close(); //Don't forget to ALWAYS close the file
      }
   }
 
   //Now customClass should be populated with all of the values from the file

 
The ParseText method would look something like this:
 
public void ParseText(string text, ref MyFile customClass) //Parse the text and fill the MyFile attr
{
      if(customClass == null)  // Make sure it's been instantiated
      {
          customClass = new MyFile();
      }
      if(text.IndexOf(":") > -1) //Make sure this is a line we should read
      {
          string[] temp = text.Split(':');
          if(temp.Length > 1) //Make sure we have at least two values
          {
              select(temp[0].Trim().ToLower()) //Lets check on the left value with no whitespace and as lowercase
              {
                   case "artist":
                      customClass.Artist = temp[1];
                      break; // In c# you MUST break each case unless you want it to trickle down
                                    
                   case "album":
                      break; // In c# you MUST break each case unless you want it to trickle down
                      customClass.Album = temp[1];
 
                   case "year":
                      break; // In c# you MUST break each case unless you want it to trickle down
                      customClass.Year = temp[1];
 
                   case "song":
                      break; // In c# you MUST break each case unless you want it to trickle down
                      customClass.Song = temp[1];
              }
          }
      }
 
}
 

I think that should about do it. Like I said, remember I typed this all in the browser window, so there could be some syntax errors. Try this out and let me know how it goes.
GeneralRe: Data Mining using C# PinmemberDavid Stone7:48 10 Oct '05  
GeneralRe: Data Mining using C# PinmemberXRaheemX8:54 10 Oct '05  
GeneralRe: Data Mining using C# PinmemberDavid Stone10:05 10 Oct '05  
GeneralRe: Data Mining using C# PinmemberXRaheemX10:46 10 Oct '05  
GeneralRe: Data Mining using C# PinmemberDavid Stone10:57 10 Oct '05  
GeneralRe: Data Mining using C# PinmemberXRaheemX11:39 10 Oct '05  
QuestionRe: Data Mining using C# Pinmemberalvinjv23:06 10 Oct '05  
AnswerRe: Data Mining using C# PinmemberXRaheemX3:09 11 Oct '05  
QuestionDynamic Image Location in Crystal Report XI Pinmemberdhol2:48 10 Oct '05  
QuestionDrawing line in c# Pinsussnewtocsharp1:20 10 Oct '05  
AnswerRe: Drawing line in c# PinmemberChristian Graus2:09 10 Oct '05  
AnswerRe: Drawing line in c# PinmemberYadav Pramod22:35 10 Oct '05  
Questiondiplaying data in datagrid without child table PinmemberRizwan Bashir0:56 10 Oct '05  
AnswerRe: diplaying data in datagrid without child table PinmembergavinJeffrey1:33 10 Oct '05  
GeneralRe: diplaying data in datagrid without child table PinmemberRizwan Bashir3:04 10 Oct '05  
GeneralRe: diplaying data in datagrid without child table PinmembergavinJeffrey3:57 10 Oct '05  
Questionplease help using references in c# Pinmemberarusmemon22:20 9 Oct '05  
AnswerRe: please help using references in c# PinmemberMaqsood Ahmed0:04 10 Oct '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 30 May 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid