|
-
Jan 9th, 2014, 09:46 AM
#1
Thread Starter
Junior Member
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.
-
Jan 9th, 2014, 09:50 AM
#2
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?
-
Jan 9th, 2014, 09:59 AM
#3
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.
-
Jan 9th, 2014, 11:31 AM
#4
Thread Starter
Junior Member
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.
Last edited by reinhold; Jan 9th, 2014 at 11:38 AM.
-
Jan 9th, 2014, 11:40 AM
#5
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.
-
Jan 9th, 2014, 11:41 AM
#6
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.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jan 9th, 2014, 11:42 AM
#7
Thread Starter
Junior Member
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.
-
Jan 9th, 2014, 11:45 AM
#8
Re: Reading and writing after a specific point in multilined text file
In fact it looks like source code
I think it's a game ini file rather than code. Presumably modding is the ultimate aim.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jan 9th, 2014, 11:45 AM
#9
Thread Starter
Junior Member
Re: Reading and writing after a specific point in multilined text file
 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 "#".
-
Jan 9th, 2014, 11:49 AM
#10
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) #
-
Jan 9th, 2014, 11:50 AM
#11
Re: Reading and writing after a specific point in multilined text file
 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!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jan 9th, 2014, 11:55 AM
#12
Thread Starter
Junior Member
Re: Reading and writing after a specific point in multilined text file
 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?
-
Jan 9th, 2014, 12:07 PM
#13
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
-
Jan 9th, 2014, 01:09 PM
#14
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|