473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting text from within a string

I’m using VB .Net 2003. I am reading a file into memory. Here is a sniplet of the file

[88games
gamename='88 Game
numPlayers=
alternating=
mirrored=
tilt=
usesService=
miscDetails=This is a 4 player team type game where 2 players compete each tim
P1NumButtons=
P1Controls=Just Buttons+butto
P1_BUTTON1=Ru
P1_BUTTON2=Jum
P1_BUTTON3=Ru

[005
gamename=00
numPlayers=
alternating=
mirrored=
tilt=
usesService=
miscDetails
P1NumButtons=
P1Controls=4-way Joystick+joy4wa
P1_BUTTON1=Fir
P1_JOYSTICK_UP=U
P1_JOYSTICK_DOWN=Dow
P1_JOYSTICK_LEFT=Lef
P1_JOYSTICK_RIGHT=Righ

[yard

It is quite large. Anyways. Now that I have it into a variable I would like to pull out a specific section into another variable. Say the “[005]” section. I have the code returning what character the string “[005]” starts at but don’t know the command or syntax to start at a certain character number (which I have) and then read until the first blank line then put it into another variable

After that I want to do the same thing, kind of, and put the parts after the “=” sign into separate labels that I have on my form

What is my best way to accomplish this

Thank you
Joh

Nov 20 '05 #1
11 1762
In article <06**********************************@microsoft.co m>, jcrouse wrote:
I?m using VB .Net 2003. I am reading a file into memory. Here is a sniplet of the file:

[88games]
gamename='88 Games
numPlayers=2
alternating=0
mirrored=1
tilt=0
usesService=0
miscDetails=This is a 4 player team type game where 2 players compete each time
P1NumButtons=3
P1Controls=Just Buttons+button
P1_BUTTON1=Run
P1_BUTTON2=Jump
P1_BUTTON3=Run

[005]
gamename=005
numPlayers=2
alternating=1
mirrored=1
tilt=0
usesService=0
miscDetails=
P1NumButtons=1
P1Controls=4-way Joystick+joy4way
P1_BUTTON1=Fire
P1_JOYSTICK_UP=Up
P1_JOYSTICK_DOWN=Down
P1_JOYSTICK_LEFT=Left
P1_JOYSTICK_RIGHT=Right

[yard]

It is quite large. Anyways. Now that I have it into a variable I would like to pull out a specific section into another variable. Say the ?[005]? section. I have the code returning what character the string ?[005]? starts at but don?t know the command or syntax to start at a certain character number (which I have) and then read until the first blank line then put it into another variable.

After that I want to do the same thing, kind of, and put the parts after the ?=? sign into separate labels that I have on my form.

What is my best way to accomplish this?

Thank you,
John


Hmm, looks like a standard windows ini file... Personally, I would
suggest using P/Invoke to call GetPrivateProfileString to get the pieces
you want. Others may disagree, but why write a parsing routine for
something the system already handles:)

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

You can read an entire section into a single buffer by passing Nothing
to the lpKeyName parameter. It will fill the buffer with all keys in
the section separated by null chars...

You're other option is to look into System.Text.RegularExpressions to
extract the piece of data you would like from the string...

--
Tom Shelton [MVP]
Nov 20 '05 #2
jcrouse wrote:
I'm using VB .Net 2003. I am reading a file into memory. Here is a
sniplet of the file:
<omitted>

It is quite large. Anyways. Now that I have it into a variable I
would like to pull out a specific section into another variable. Say
the "[005]" section. I have the code returning what character the
string "[005]" starts at but don't know the command or syntax to
start at a certain character number (which I have) and then read
until the first blank line then put it into another variable.
You probably don't want to be doing this in memory, especially since you say
the file is quite large.
Why not instead read the file line by line using StreamReader.ReadLine, and
parse the file as you go. Just keep reading lines and discarding them until
you get the line that reads "[005]", then read the lines after that parsing
them until you get to a blank line.
After that I want to do the same thing, kind of, and put the parts
after the "=" sign into separate labels that I have on my form.


You'd want to use String.Split for that.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #3
That's all well and dandy but I need code. As earlier stated, I can't seem to store or retrieve anything but the starting character numer of the beginning of the matching expression. How do I do a StreamReader.ReadLine and then put it into a varaible or label.text property

Thank you
John
Nov 20 '05 #4
What do I do with your sample code. Pardon me for asking, I'm a little confused and new at this. How do I invoke it in startup? I obviously need to fill the variables somehow

HELP
John
Nov 20 '05 #5
Tom....Here is my code. It keeps underlining the word declare and saying that "Keyword is not valid as an identifier"? I'm stumped

Joh
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Clic

Public Declare Function GetPrivateProfileString Lib "kernel32"
(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As System.Text.StringBuilder,
ByVal nSize As Integer,
ByVal lpFileName As String) As Intege

lpAppName = lblInput.Tex
lpKeyName = "P1_BUTTON1
lpDefault = "
lpReturnedString = (256
lpNsize = Len(lpReturnedString
lpFileName = "C:\CPViewer\Controls.ini
End Su

Nov 20 '05 #6
John,
Put the declare outside the Sub! As the Declare is used to declare a
Function or a Sub that is external to the program, remember that VB does not
allow subs & functions to be nested inside of other subs or functions.
Public Declare Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Hope this helps
Jay

"jcrouse" <an*******@discussions.microsoft.com> wrote in message
news:BF**********************************@microsof t.com... Tom....Here is my code. It keeps underlining the word declare and saying that "Keyword is not valid as an identifier"? I'm stumped.
John

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Public Declare Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

lpAppName = lblInput.Text
lpKeyName = "P1_BUTTON1"
lpDefault = ""
lpReturnedString = (256)
lpNsize = Len(lpReturnedString)
lpFileName = "C:\CPViewer\Controls.ini"
End Sub

Nov 20 '05 #7
Well...here's my code

Private Declare Auto Function GetPrivateProfileString Lib "kernel32"
(ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As System.Text.StringBuilder,
ByVal lpNsize As Integer,
ByVal lpFileName As String) As Intege

Dim x As Lon

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Clic

lpAppName = "1942
lpKeyName = "P1_BUTTON1
lpDefault = "
lpReturnedString = (256
lpNsize = Len(lpReturnedString
lpFileName = "C:\CPViewer\Controls.ini

x = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, lpNsize, lpFileName

lblOutput.Text =

End Su

Now I get an error on button click that says

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.ex
Additional information: Specified cast is not valid

Anymore ideas? I'm close here. I think the parameters for the buffer may be wrong
John
Nov 20 '05 #8
see inline...
"jcrouse" <an*******@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
Well...here's my code:

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal lpNsize As Integer, _
ByVal lpFileName As String) As Integer

Dim x As Long

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
lpAppName = "1942"
lpKeyName = "P1_BUTTON1"
lpDefault = ""
lpReturnedString = (256) <<==== what is this?????
try instead
lpReturnedString = New System.Text.StringBuilder(256)

then this lpNsize = Len(lpReturnedString)
becomes
lpNsize = 256

or if you like

lpNsize = lpReturnedString.Length
lpFileName = "C:\CPViewer\Controls.ini"

x = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, lpNsize, lpFileName)
lblOutput.Text = x

End Sub

Now I get an error on button click that says:

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.exe Additional information: Specified cast is not valid.

Anymore ideas? I'm close here. I think the parameters for the buffer may be wrong. John

Nov 20 '05 #9
On Wed, 26 May 2004 19:16:03 -0700, jcrouse wrote:
Tom....Here is my code. It keeps underlining the word declare and saying that "Keyword is not valid as an identifier"? I'm stumped.

John

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

Public Declare Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

lpAppName = lblInput.Text
lpKeyName = "P1_BUTTON1"
lpDefault = ""
lpReturnedString = (256)
lpNsize = Len(lpReturnedString)
lpFileName = "C:\CPViewer\Controls.ini"
End Sub


Public Declare Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn1.Click

Dim lpReturnedString As New System.Text.StringBuilder(256)

GetPrivateProfileString( _
"88games",
"P1_BUTTON1",
String.Empty,
lpReturnedString,
lpReturnedString.Capacity,
"C:\CPViewer\Controls.ini"

MessageBox.Show("P1_BUTTON1=" & lpReturnedString.ToString())
End Sub

HTH
--
Tom Shelton [MVP]
Nov 20 '05 #10
* "=?Utf-8?B?amNyb3VzZQ==?=" <an*******@discussions.microsoft.com> scripsit:
Now I get an error on button click that says:

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.exe
Additional information: Specified cast is not valid.


Use this code instead:

<URL:http://www.mentalis.org/soft/class.qpx?id=6>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #11
Thank you....that did it

Much appreciated
John
Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DC Gringo | last post by:
I am a bit twisted and need some straightening out. I have a webform with 3 controls on it: a mainHeader, a sectionHeader with a label control contained in the mainHeader, and a topHeadline. In...
10
by: David B | last post by:
I am writing a console App with VB 2005 where I accept a text file as input, parse it and insert the text data into an SQL table. I have written the code correctly as well as I can tell because it...
1
by: Jerry John | last post by:
I am working in ASP.NET with C#. I have a text file which contains datas with delimiters. For example:- MSH|^~\$|DISCHARGE|CLAY COUNTY MEMORIAL|||200502110939| I also have an XML file created...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
3
by: GaryDean | last post by:
I have a button field in a gridview defined as follows: <asp:ButtonField ButtonType="Button" CommandName="AcceptOrder" Text="Accept Whole Order"> <ControlStyle Font-Size="X-Small" />...
12
by: Angus | last post by:
I am writing a class as a wrapper around a std::map. The class is: template<class TKey, class TValue> class CGeneralMap : protected map<TKey, TValue> At the moment I have basic functions like...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
3
by: MDCdeveloper | last post by:
Hello all, and thank you in advance for any feedback/help you might post. I need help getting a web page element id out of a string variable to be used within my c# code behind page. I am...
2
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.