Reading and writing after a specific point in multilined text file
Hi,
I need help to write a code which has to read and write numerical values from a text file before semicolon sign.
The textfile has multiple lines and goes like this:
option1 56;
option2 721;
option3 -1;
option4 0;
It should not read and write anything before the space and after the semicolon, only the numbers. Numbers can be negative or up to 3 digits.
Re: Reading and writing after a specific point in multilined text file
Well what have you tired? Have you read the documentation for io.file.readalllines class?
Re: Reading and writing after a specific point in multilined text file
Try something like this:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Function GetNumbers(ByVal path As String) As List(Of Integer)
Dim l As New List(Of Integer)
Dim lines() As String = IO.File.ReadAllLines(path)
For Each word As String In lines
word = word.Remove(0, word.IndexOf(" "c) + 1)
word = word.Replace(";"c, String.Empty)
Dim i As Integer
If Integer.TryParse(word, i) Then l.Add(i)
Next
Return l
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using ofd As New OpenFileDialog
With ofd
.FileName = String.Empty
.Filter = "Text|*.txt"
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
.Multiselect = False
.Title = "Open File"
End With
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim numbers As List(Of Integer) = GetNumbers(ofd.FileName)
For Each i As Integer In numbers
ListBox1.Items.Add(i.ToString)
Next
End If
End Using
End Sub
End Class
I use a button to call the function and a listbox to display the numbers.
Re: Reading and writing after a specific point in multilined text file
Hey dday9 thanks for reply and looks like it should be working in theory but in my case all i get is an empty list.
Here's the actual text file i'm trying to get values from: http://pastebin.com/hpcz3USm
BTW, this list is static and lines will always stay the same.
Re: Reading and writing after a specific point in multilined text file
Basically the way that the function works is it takes a string in this format:
| Keyword |
Description |
| word |
Some word |
| space |
Represents a blank space |
| number |
The value you're trying to get |
| semi-colon |
Represents the end of the line |
-or-
If your text file deviates from that format, then the way that the function is structured needs to reflect that deviation. Now I took a look at your text file and it is not setup in that format that you gave at all. In fact it looks like source code, which I'm not comfortable helping out anymore if you're trying to get values from a source code because if you created that source then you should know the values needed.
Re: Reading and writing after a specific point in multilined text file
You're kidding, right? You ask for a solution to reading a text that bears absolutely no resemblance to the one you actually work with and then are surprised that it doesn't give you the result you wanted! After you've apologised profusely to dday I suggest that you decide exactly what it is you want to extract from where in the file and give us that information in as much detail as possible.
Re: Reading and writing after a specific point in multilined text file
Nah it's not a source code, it's the file where all preferences of a game is stored. It is generated automatically by the game under My Documents folder.
Re: Reading and writing after a specific point in multilined text file
Quote:
In fact it looks like source code
I think it's a game ini file rather than code. Presumably modding is the ultimate aim.
Re: Reading and writing after a specific point in multilined text file
Quote:
Originally Posted by
dunfiddlin
You're kidding, right? You ask for a solution to reading a text that bears absolutely no resemblance to the one you actually work with and then are surprised that it doesn't give you the result you wanted! After you've apologised profusely to dday I suggest that you decide exactly what it is you want to extract from where in the file and give us that information in as much detail as possible.
I beg your pardon? Absolutely no resemblence? Did you even bother to check and compare the files? The only difference between the files is the comments for each setting starting and ending with "#".
Re: Reading and writing after a specific point in multilined text file
How how does a textfile of the likes of:
option1 56;
option2 721;
option3 -1;
option4 0;
Look any thing like:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #
x_res 1920; # x_res <int32>, Fixed window width #
y_res 1080; # y_res <int32>, Fixed window height #
x_pos 0; # x_pos <int32>, Window position #
y_pos 0; # y_pos <int32>, Window position #
vfs_log_level 0; # vfs_log_level <int32>, 0 - off, 1 - mod-user, 2 - dev #
unit_test false; # unit_test <bool>, unit test (for daily build) #
gfx_first_run false; # gfx_first_run <bool>, First time application run #
gfx_video_memory 0; # gfx_video_memory <int>, Override available video memory (bytes) #
Re: Reading and writing after a specific point in multilined text file
Quote:
Originally Posted by
reinhold
I beg your pardon? Absolutely no resemblence? Did you even bother to check and compare the files? The only difference between the files is the comments for each setting starting and ending with "#".
Yes, absolutely no resemblance! Even if there is a single line in there that has the exact pattern ...
option1 56;
option2 721;
option3 -1;
option4 0;
(and I certainly can't see one!) it would still not be possible to devise an extraction routine because there are so many lines that do not follow this pattern. If you truly believe that you gave an accurate description of the file contents then ... well, words fail me!
Re: Reading and writing after a specific point in multilined text file
Quote:
Originally Posted by
ident
How how does a textfile of the likes of:
option1 56;
option2 721;
option3 -1;
option4 0;
Look any thing like:
write_preferences_at_exit true; # write_preferences_at_exit <bool>, Write preferences at exit #
app_multirun false; # app_multirun <bool>, Allow multiple instances of the application #
x_res 1920; # x_res <int32>, Fixed window width #
y_res 1080; # y_res <int32>, Fixed window height #
x_pos 0; # x_pos <int32>, Window position #
y_pos 0; # y_pos <int32>, Window position #
vfs_log_level 0; # vfs_log_level <int32>, 0 - off, 1 - mod-user, 2 - dev #
unit_test false; # unit_test <bool>, unit test (for daily build) #
gfx_first_run false; # gfx_first_run <bool>, First time application run #
gfx_video_memory 0; # gfx_video_memory <int>, Override available video memory (bytes) #
You can trim off parts start and end with "#" (because they are just comments and are not needed) which will give us this:
Code:
write_preferences_at_exit true;
app_multirun false;
x_res 1920;
y_res 1080;
x_pos 0;
y_pos 0;
vfs_log_level 0;
unit_test false;
gfx_first_run false;
gfx_video_memory 0;
And the format we end up with is this:
[TEXT][BLANK][NUMERIC VALUE][;]
Which is the same as I stated in the beginning of this thread:
Code:
option1 56;
option2 721;
option3 -1;
option4 0;
See was that so hard?
Re: Reading and writing after a specific point in multilined text file
If that's the case then do the same thing in my function, only removing any comments. The way my function is set up is if the value isn't an integer, then it won't add it to the list. So in this example:
Code:
write_preferences_at_exit true;
app_multirun false;
x_res 1920;
y_res 1080;
x_pos 0;
y_pos 0;
vfs_log_level 0;
unit_test false;
gfx_first_run false;
gfx_video_memory 0;
It will only return:
For the: x_res, y_res, x_pos, y_pos, vs_log_level, gfx_video_memory
Re: Reading and writing after a specific point in multilined text file
As always i'l tackle it with regex
vb Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Foo()
Dim rx As New Regex(" -?\d{1,3}[^;]")
Dim lines As String() = IO.File.ReadAllLines("C:\test.txt")
Dim matches =
(
From line In lines
Where rx.IsMatch(line)
Select rx.Match(line).Value
)
IO.File.WriteAllLines("C:\numbers.txt", matches)
End Sub
End Class