Results 1 to 14 of 14

Thread: Reading and writing after a specific point in multilined text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    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.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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?
    My Github - 1d3nt

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    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.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    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-
    Code:
    word number;
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading and writing after a specific point in multilined text file

    Here's the actual text file i'm trying to get values from: http://pastebin.com/hpcz3USm
    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!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    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.

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Reading and writing after a specific point in multilined text file

    Quote Originally Posted by dunfiddlin View Post
    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 "#".

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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) #
    My Github - 1d3nt

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading and writing after a specific point in multilined text file

    Quote Originally Posted by reinhold View Post
    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!

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    24

    Re: Reading and writing after a specific point in multilined text file

    Quote Originally Posted by ident View Post
    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?

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    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:
    Code:
    1920
    1080
    0
    0
    0
    0
    For the: x_res, y_res, x_pos, y_pos, vs_log_level, gfx_video_memory
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Reading and writing after a specific point in multilined text file

    As always i'l tackle it with regex

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Foo()
    6.         Dim rx As New Regex(" -?\d{1,3}[^;]")
    7.         Dim lines As String() = IO.File.ReadAllLines("C:\test.txt")
    8.         Dim matches =
    9.             (
    10.                 From line In lines
    11.                 Where rx.IsMatch(line)
    12.                 Select rx.Match(line).Value
    13.              )
    14.  
    15.  
    16.         IO.File.WriteAllLines("C:\numbers.txt", matches)
    17.     End Sub
    18.  
    19. End Class
    My Github - 1d3nt

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
  •  



Click Here to Expand Forum to Full Width