text file read

G

Guest

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
 
V

Voorshwa

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
 
S

sloan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top