Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 3rd, 2007, 02:03 AM
Registered User
 
Join Date: Nov 2007
Location: , , USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Fixed Width text file to MSDE with C#

Greetings,

I am new to C# and have not programmed since my ALGOL days.

I have been doing a lot of reading, but can not figure out and efficient way to directly import a fixed width text file with 8 fields only separated by their position in the string. Each line in the text file is 420 characters long.

Some of the fields have extraneous \n's in the field so I would like to read them into a MSDE SQL database, separated into separate character fields and work on cleaning up the line feed characters after extracting them from the SQL database.

Thanks for your help,

Mike



  #2 (permalink)  
Old November 3rd, 2007, 05:28 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You could use regular expressions to parse each line.

A pattern like "(.{8})(.{12})" would capture the first 8 characters, then the next 12. Do this for the entire line. Make sure to set the MultiLine option so that "." matches those line breaks as well.



/- Sam Judson : Wrox Technical Editor -/
  #3 (permalink)  
Old November 9th, 2007, 02:50 AM
Registered User
 
Join Date: Nov 2007
Location: , , USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sam,

Thanks so much for your reply.

I am trying to decipher regular expressions and how to use them. Do you have any code snippets that show how to implement what you suggest?

Also, what is the best way to import a 420 character line from the text file. I was originally importing the file as one long string using the \b\n to copy the each text line into a table and using string functions to parse the code.

Unfortunately the data entry people are inserting extra \n characters into the file and the parsing doesn't work.

I am now trying to figure a way to take 420 characters at a time and load them into the table.

Thanks,

Mike

  #4 (permalink)  
Old November 9th, 2007, 05:09 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Well the StreamReader contains a method called ReadBlock, which can be used to read 420 characters. Or you could just use it to read each n characters.

string col1 = streamReader.ReadBlock(8);
string col2 = streamReader.ReadBlock(12);
...

if( streamReader.Peek() == -1 ) // no more lines.

/- Sam Judson : Wrox Technical Editor -/


Similar Threads
Thread Thread Starter Forum Replies Last Post
write text file with fixed field length hugoscp Classic ASP Professional 0 June 27th, 2007 06:12 AM
CSS for fixed width li and right aligned text echovue CSS Cascading Style Sheets 2 March 12th, 2007 02:21 PM
Import a fixed width txt file to access subhanak Access VBA 12 May 5th, 2006 05:35 AM
Automate import (fixed width) U.N.C.L.E. Access VBA 0 February 3rd, 2006 06:55 PM
DTS Fixed Width File Import rochak SQL Server DTS 3 September 26th, 2005 09:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.