Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

text file read

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CsharpGuy - 13 Feb 2006 20:40 GMT
I have to read a text file and parse it out to load to a db and I'm having
some issues in doing it. here is what the text file looks like

Dealership number:  98665362236
Location:   Maryland
Owner: John Smith
Type: Luxury
BMW, 325i, VIN12363625252362, New,, 20051212. 35400, Ocoonner
Lexus, IS300, VIN6936363633363. Used, Small dent on driver side, 20050112,
24000, JSmith

I need to account the Dealership number
then get the other information starting with BMW, how can I skip line 2 thru
4 and still get teh dealership number and the lines starting with BMW?
so the file is
Make, Model, VIN#, New/Used, any details, date sold,. price and salesmen
Voorshwa - 13 Feb 2006 20:51 GMT
Well this might be a bit of a brute force method, but if this file is
line by line then couldn't you just read each line into a cell in an
array?  Then you could just pick the cells you need to read into your
DB columns?  I think that would be the easiest if the file is
segregated into lines like you describe.

HTH

V
sloan - 13 Feb 2006 21:05 GMT
When you load a textfile in C#, you get access to a ReadLine() method.

There's no rocket science here.
You need to ReadLine, and look for things you can bank on.

using System;
using System.IO;

class Test
{
   public static void Main()
   {
       try
       {        bool isANewCar  = false;    int lineRowPerSingleCar = 0;
           // Create an instance of StreamReader to read from a file.
           // The using statement also closes the StreamReader.
           using (StreamReader sr = new StreamReader("TestFile.txt"))
           {
               String line;
               // Read and display lines from the file until the end of
               // the file is reached.
               while ((line = sr.ReadLine()) != null)
               {
                   lineRowPerSingleCar +=1;                    if
(line.Substring(0,10).ToUpper() == "DEALERSHIP"{        // figure out a way
to get the last characters, perhaps on the ":"        isANewCar =
true;lineRowPerSingleCar = 0; //reset}            switch
(lineRowPerSingleCar){        case 2:    case 3:    case 4:        //do
nothing    break;    default:       // do somehting else       break;}
               }
           }
       }
       catch (Exception e)
       {
           // Let the user know what went wrong.
           Console.WriteLine("The file could not be read:");
           Console.WriteLine(e.Message);
       }
   }
}

It's ugly as sin, but that's how you read files, where the format is a
little whack.
Looking at it, I'd use "Dealership number" to signal a new car.  And reset
the lineCounter based on that.
Ignore 2, 3, 4, and do something with the rest.

Perhaps the sample above can get you going.

> I have to read a text file and parse it out to load to a db and I'm having
> some issues in doing it. here is what the text file looks like
[quoted text clipped - 12 lines]
> so the file is
> Make, Model, VIN#, New/Used, any details, date sold,. price and salesmen

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.