| View previous topic :: View next topic |
| Author |
Message |
LtSchofield Newbie
Joined: 07 Sep 2005 Posts: 4
|
Posted: Sep 7th, 2005 06:31 AM Post subject: File reading |
|
|
i have vb 6 and i am creating a program. i am using the string functions in order to parse through a text file. but my problem is that i open the file using | Code: | | open "filename.extension" for input as File | ( file is an integer and after it was created i used the function FreeFile to get the value.) but when i try and convert it to a string so i can use the InStr function to search for key things, using
| Code: | | Input #File, StrFile |
StrFile being a string created earlier.
the value of StrFile is just a bunch of ******************** those.
i was thinking it may be because the text in the file is non-deliminated text and it may not be reading it like a string but i don't know and if it is i don't know what to do.
can anyone help me. thanks in advance. |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Sep 7th, 2005 09:44 AM Post subject: |
|
|
Hi and welcome to the forum,
You've got things a bit upside down !!
FreeFile returns a filenumber that is available to use. You'd normally do something like:
intFile = FreeFile
Open "filename.ext" for input as intFile
Input #intfile, strData
Regards
Doug |
|
| Back to top |
|
LtSchofield Newbie
Joined: 07 Sep 2005 Posts: 4
|
Posted: Sep 8th, 2005 07:31 AM Post subject: |
|
|
sorry mabey i didn't make myself clear enough my fault but thats what i was doing. i got it working though, it turns out that when i used the command
input "file number", "variable"
it was only giving me the first line of the file which was a bunch of astrixes. so i ended up using a loop with the line input fuction and Concatenation to store the file in a string. if anyone knows a way to tell the program to take the whole file could you tell me.
and i have two last questions. what is the funtion used for placing a line break in. in a string. ie in c++ its
endl or "\n"
is there one for vb. secondly is there a way with strings and files to take a charater in the file/string one by one. so if you had a string with "random string stuff" to extract the r in a string test it, then extract the a test it and so on.
thanks for the quick reply Doug. |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Sep 8th, 2005 08:14 AM Post subject: |
|
|
Hi,
Ok glad you've got that bit going.
In answer to you other questions:
1. To get the whole file you could use: strBuff = Input(LOF(1),#1) which would put the entire contents of the file in strBuff.
2. The Linebreak in VB is vbLf
3. The Mid(string,startpos,length) function will return the characters starting at 'startpos' for a length of 'length' within string 'string')
eg.
| Code: |
for lgni = 1 to len(strBuff)
strTemp = mid(strBuff,lngi,1)
if strTemp = "?" then
etc
endif
next inti
|
Would go through the entire string picking up one character at a time.
If you're searching for a specific sub-string in a string you can use the InStr function:
lngx = InStr ([start,] string1,string2)
which will look for 'string2' in 'string1' starting at position 'start' in 'string 1'. "start," is optional.
lngx = position in string1 where string2 starts
Otherwise lngx = 0 (ie string not found or one or other were null or start was outside the range of string1)
There are also Left and Right functions (eg Left(string,length) returns the leftmost 'length' characters from 'string')
Note that all these string functions require LONG variables for numeric parameters and return values. According to the VB Programmer's Guide, strings can be 64K in length in 16bit architecture and 2 ^31 in 32bit architectures.
Hope this helps
Good luck and regards
Doug |
|
| Back to top |
|
LtSchofield Newbie
Joined: 07 Sep 2005 Posts: 4
|
Posted: Sep 9th, 2005 03:27 AM Post subject: |
|
|
yeah thanks.
i no about InStr but it can't be used in this situation so i'll aske u if there's a quicker way than one char at a time
the file contains alot of info about levels in a game.
each level starts with $ followed by the level name then # then the author then # then level type then # and the data
i need to find and store the name of the levels. i already have part of the program that goes through and counts how many levels using the InStr function and finding $ but when i find that i need to store all the letters between that and the # in a string. anyway to do that instead of looking at each letter and testing if it's # and if not store and keep going. cause i no in c++ they have a function that lets u take all characters up to a certain character specified.
thanks for the help |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Sep 9th, 2005 03:54 AM Post subject: |
|
|
Hi,
You should be able to do that with Instr and Mid:
| Code: |
Dim lngStart As Long
Dim lngPosDol As Long
Dim lngPosHash As Long
lngStart = 1
Do Until lngStart = > Len(strData)
lngPosDol = Instr(lngStart, strData,"$")
If lngPosDol <> 0 Then
'
' Found a $, now look for the first "#" to follow
'
lngPosHash = Instr(lngPosDol,strData,"#")
If lngPosHash <> 0 Then
Text1.Text = Text1.Text & Mid(strData,lngPosDol + 1,lngPosHash-lgnPosDol-1) & vbCrLf
Else
'
' $ found but no # found in the string
'
Msgbox "Unable to find a Level Name"
Exit Sub
End If
Else
'
' Unable to find a Dollar
'
Msgbox "No Level information Available"
Exit Sub
End If
'
' Start looking for a $ just after the end of the last level name
'
lngStart = lngPosHash + 1
Loop
End Sub
|
This will (should) put all the Level Names on different lines of a multiline text box (Text1) _________________ If you can see the light at the end of the tunnel, it probably means there's a Train coming. |
|
| Back to top |
|
LtSchofield Newbie
Joined: 07 Sep 2005 Posts: 4
|
Posted: Sep 9th, 2005 10:08 PM Post subject: |
|
|
| thanks for that. i would have never thought to do it like that and it would have taken me much longer. great advice thanks again. |
|
| Back to top |
|
VB_Developer Newbie
Joined: 25 Feb 2006 Posts: 18 Location: Hyderabad, India
|
Posted: Feb 27th, 2006 03:16 AM Post subject: |
|
|
Hi,
You can even use the Replace(), built in function in VB to replace any char or work in a string.
Thanks
Deepak |
|
| Back to top |
|
|