Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Reading Delimited text files


Reply
 
Thread Tools Display Modes
  #1  
Old 06-02-2010, 08:46 AM
Carlmundo Carlmundo is offline
Newcomer
 
Join Date: Jun 2010
Posts: 4
Default Reading Delimited text files


Basically say I have a text file with the following contents:

"Tim", "USA", "1982"
"Jane", "Germany", "1980"
"Brad", "France", "1989"

Can I have a ListBox for each field, one listbox for Names, one for countries, one for years?

Thanks in advance.
Reply With Quote
  #2  
Old 06-02-2010, 08:50 AM
lordofduct's Avatar
lordofduct lordofduct is offline
Centurion
 
Join Date: May 2010
Location: south florida yo
Posts: 120
Default

yes...

you have to parse your data how ever you decide to. For instance it looks like you group by three per line. So you can delimit by line (read each line for each entry) and then split that line up at the ',' for the delimeter... 1st entry in that split is the name, 2nd the country, 3rd the year.

But of course you know this, that's the schema you set up.

So beyond IF it is possible to store strings in a ListBox... which obviously you can, it's kind of the purpose of them. What is it you need to know?
__________________
I'm a turtle... woooo
Reply With Quote
  #3  
Old 06-02-2010, 10:11 AM
Carlmundo Carlmundo is offline
Newcomer
 
Join Date: Jun 2010
Posts: 4
Default

I just need to know how I would go about making a list box for each Name, Country and Year.
Reply With Quote
  #4  
Old 06-02-2010, 10:18 AM
gamernixin's Avatar
gamernixin gamernixin is offline
Regular
 
Join Date: Nov 2006
Posts: 80
Default

string = "blah,blah2,blah3"

string.split(",")

will take blahblah2blah3.
i would use different seperators. like

string = "blah,blah2.blah3;"
and make a split for each.. " , . ; "

But i bet there is an easier method.
Reply With Quote
  #5  
Old 06-02-2010, 10:24 AM
Carlmundo Carlmundo is offline
Newcomer
 
Join Date: Jun 2010
Posts: 4
Default

Thanks, got it going now
Reply With Quote
  #6  
Old 06-02-2010, 11:02 AM
gamernixin's Avatar
gamernixin gamernixin is offline
Regular
 
Join Date: Nov 2006
Posts: 80
Default

Thats great, how did you do it then?

( I just like when people post their solutions after solving a problem so other people can study it )

Edit:
Also, you could use a more complex method, by using the string.substring, and/or string.indexof.
example.

string = "Name:Peter Age:18"
find Name: in the string, and it's position, then start reading a substring from it's end point, untill it reaches the start point of the next indexof which would be Age:.

Just a thought.
I'm going to use that for my project and i thought i would share it. As delimiters might be messed up if you actually want a specific character in the string, like "th!s" and you split by !, you would get "th" and "s". :b

Last edited by gamernixin; 06-02-2010 at 03:20 PM. Reason: I figured another way.
Reply With Quote
  #7  
Old 06-14-2010, 08:31 AM
CrashPilot CrashPilot is offline
Regular
 
Join Date: Jun 2009
Location: Netherlands
Posts: 73
Default

Seems this thread covers the splitting strings portion so i'll post my next problem here.

I've got a server running that gets some string via sockets. this work perfectly fine. Now i want the string received in two: ex. :

String that came in = TYPE I
I want to split it at the " " (space) so i get:

Command = TYPE
Argument = I (yes it is a FTP server.

The most obvious solution that i see atm is using the string.split method and I use this to split the string yet some very strange behaviour results from this. The code follows below:

Code:
Public Function ProcessCommand(ByVal FTPCommand As String) As Boolean
Dim SplittedCommand As String = { Nothing, Nothing } If FTPCommand = Nothing Then FTPCommand = "" SplittedCommand = FTPCommand.Split( Ctype(" ", Char)) Select Case SplittedCommand(0).ToUpper()
Case "PASV"
SendMessage("227 Entering Passive Mode(123.456.789.456.10)")
Case "TYPE"
SendMessage("200 Type set to I")
End Select End Sub
The parameter "FTPCommand" that is passed to this function is aquired from the client via a TCP socket. The bytes received there are used to convert the data to a string by using the UTF8.GetString method. Code:

Code:
Private Sub HandleClient()
Dim FTPCommand As String = String.Empty Dim ReceiveBuffer() As Byte While Not FTPCommand.ToUpper = "QUIT"
ReDim ReceiveBuffer(Me.ControlSocket.ReceiveBufferSize - 1) Me.ControlSocket.Receive(ReceiveBuffer, ReceiveBuffer.Length, SocketFlags.None) FTPCommand = Encoding.UTF7.GetString(ReceiveBuffer) If Not ProcessCommand(FTPCommand) Then
Debug.Print("Process command failed") Exit While
End If
End While
Me.m_ControlSocket.Close()[/INDENT]End Sub
Now the splitting of the string and then selecting a case for it, works only if the string can be splitted in more then 1 substring:

TYPE I --> This works.
If i display the first substring in a textbox or debug window it displays normally

PASV -->This doesn't.
If i display the first (and only) substring in a textbox or debug window it displays normally!!

So why can a textbox handle this substring and the select case is completely oblivious to it?????? I hear you thinking, check it while debugging, ok i did that and there is one thing that i noticed:

TYPE I: "TYPE"... and "I {Length=2} (note the quotionmarks are missing next to the "I")
PASV: "PASV {Length=1} (again the quotemarks are missing)

I don't know if this is normal or not but if i simply use the substring method on the first four characters it all works perfectly so it must be a glitch in the split string method. Anyone know why?
__________________
-- I divided by zero... and survived --
Reply With Quote
  #8  
Old 06-14-2010, 08:50 AM
lordofduct's Avatar
lordofduct lordofduct is offline
Centurion
 
Join Date: May 2010
Location: south florida yo
Posts: 120
Default

I don't even know how that code compiled for you, you must not have strict mode turned on.

Here, cleaned up:

Code:
Public Function ProcessCommand(ByVal FTPCommand As String) As Boolean
	Dim split() As String ''it needs to be a string array, not a string.

	split = FTPCommand.Split(" ")'' you don't need to force type a string to a char, if it's one char long, it's a char

	Select Case split(0).ToUpper()
		Case "PASV"
			SendMessage("227 Entering Passive Mode(123.456.789.456.10)") 
		Case "TYPE"
			SendMessage("200 Type set to I")
	End Select

	''it's a function, return something
	Return True
End Function ''you opened it as a function, so end it as a function... ugh I hate VB's verbosity
note, your var 'StrippedCommand' was typed to a string... NOT a string array. We need it to be an array! Why? Because if it's just a regular old string, using the (#) accessor gives us a sub character at that index in the string (hence why your case loop fails...)
__________________
I'm a turtle... woooo
Reply With Quote
  #9  
Old 06-15-2010, 01:28 AM
CrashPilot CrashPilot is offline
Regular
 
Join Date: Jun 2009
Location: Netherlands
Posts: 73
Default

Hey lordofduct,

Ty for pointing out those errors in my post, next time i'll try to be more carefull when typing the code :P. Unfortunatly that isn't the actual code i have in the program and there I do use an array of strings in order to split it and i do return true or false depending if the case finds a valid command (or not). Those aren't the problem. Currently I even fail at concatenating filepath info and filename info:

STOR \16-02-1983 14_1_45.csv with c:\MyApp\

The debugger shows that all is fine but it will not let me save the file due to an invalid character in the filepath. Now any other program can save that just fine thus leads me to believe that something goes wrong when receiving the commands from the client. Somehow a nasty character that isn't visible in debugging sneaks into the string and destroy's my program.

I wonder if there is some sort of flush method or something like this that i should call that i missed however i cannot remember using it when using sockets, only when writing encrypted text with, for example: 3DES

I will post the exact code of that portion down here:

Code:
'Handles client communications on port 21
Private Sub HandleClient()
Dim FTPCommand As String = String.Empty Dim ReceiveBuffer() As Byte 'As long as the quit command is not given: While Not FTPCommand.ToUpper = "QUIT"
ReDim ReceiveBuffer(Me.ControlSocket.ReceiveBufferSize - 1) 'Resize buffer for receiving data from socket 'If more then 0 bytes have been received, then process the possible command If Me.ControlSocket.Receive(ReceiveBuffer, ReceiveBuffer.Length, SocketFlags.None) > 0 Then
FTPCommand = Encoding.ASCII.GetString(ReceiveBuffer,0,ReceiveBuffer.Length ) 'Translate to string 'Pass it to the command processor. If the command fails, close client etc. etc. If Not ProcessCommand(FTPCommand) Then
Debug.Print("Process command failed") Exit While
End If
End If
End While Me.m_ControlSocket.Close()
End Sub
Increasing the buffer doesn't work, different UTF7 - 8 etc doesn't work. .Trim doesn't work. What am i missing??

--EDIT--

It's official, something is messing up the commands that come in from the client. I hacked the string by using:

Code:
Regex.Replace(FTPCommand,"[^a-z,A-Z,0-9, ,-,_]","")
And boom and everthing works. I do still wonder what character is messing up the system though.

--EDIT--: Solution

Seems the ftp client sending the data included a cariagereturn + linefeed into his command. Aparently the getstring method:
ASCII.GetString(Bytes) does not remove these and the fun starts. Adding the next line of code solves the problem:

Code:
FTPCommand = Encoding.ASCII.GetString(ReceiveBuffer, 0, Bytes).TrimEnd(ControlChars.CrLf.ToArray)
Funny (and anoying) how such a little thing can cost you a day.
__________________
-- I divided by zero... and survived --

Last edited by CrashPilot; 06-15-2010 at 02:47 AM. Reason: Confirmed Problem
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->