Results 1 to 13 of 13

Thread: How to introduce line break each 12 words when writing a file'?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    39

    Question How to introduce line break each 12 words when writing a file'?

    Hi! I have the following code to read a FlexGrid and save the data in a txtFile. I want to introduce a break line each 12 parameters written that are separated by " ; ".

    My code:

    Code:
    Private Sub Save_Data_Click()
          
        Dim ret As Boolean
      
        ' Le envia el control MsFlexgrid, el path del archivo txt y el delimitador
        ret = Export_Data(Grid1, "C:\Documents and Settings\Administrador\Escritorio\TFG\Registros\Register_Data.txt", Chr(59))
          
        If ret Then
            MsgBox "Table exported! ", vbInformation
        End If
    End Sub
    
    
    Public Function Export_Data(MSFlexGrid As Object, Path_Txt As String, Delimitador As Variant) As Boolean
      
        On Error GoTo Funcion_Error
        Dim i, ja As Integer
        Dim Free_File As Integer
       
        ' NĂºmero de  archivo libre para crear el archivo de texto
        Free_File = FreeFile
        ' Abre y crea el  archivo
        Open Path_Txt For Output As #Free_File
        
        Print #Free_File, "#MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#"
          
        ' Recorre las filas del Flexgrid
        For i = 1 To Grid1.Rows - 1
            Grid1.Row = Fi  'Empiezo en la fila 1 no en la cero
              
            For j = 1 To Grid1.Cols - 1
                Grid1.Col = j
         
    
                ' Write the data
                'Print #Free_File, vbNullString & Grid1.text & vbNullString;
                            If j >= 1 Then
                                 Print #Free_File, Grid1.text & Delimitador;
                             End If
    
            Next
              
            'Print #Free_File, ""
          
        Next
        Close
        Exportar_Datos = True
          
        ' Fin
        Exit Function
      
    ' error
    Funcion_Error:
        Close #Free_File
        MsgBox Err.Description, vbCritical
    End Function
    What it shows in the file:

    Code:
    #MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#   (1sr line)
    ;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps-20MHz/1S;1131,1109;26m4s;210ms;-62@1Mbps;25 (2nd line)  dB;99%;60958;192.168.100.253;CCK:1-11;;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps- (2nd line)  
     20MHz/1S;1207,1185;27m10s;440ms;-32@6Mbps;55 dB;96%;60488;192.168.100.253;CCK:1- (2nd line)  11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  (2nd line) 
    And I want:

    Code:
    #MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#   (1sr line)
    ;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps-20MHz/1S;1131,1109;26m4s;210ms;-62@1Mbps;25 (2nd line)  dB;99%;60958;192.168.100.253;CCK:1-11; (2nd line)
    ;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps- (3rd line)  
     20MHz/1S;1207,1185;27m10s;440ms;-32@6Mbps;55 dB;96%;60488;192.168.100.253;CCK:1- (3rd line)  11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  (3rd line) 
    The ;;;;;;;;;;;;;;;; are blank cells. It would be a great idea delate it too....
    Thank you!

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: How to introduce line break each 12 words when writing a file'?

    Hi ams16,

    I didn't study your code, but I did study the two text files you posted. Also, you didn't post a screen-shot of your FlexGrid, so I'm not totally clear as to what we're looking at.

    However, on the FlexGrid, it appears that you have a single row that you wish to break up into multiple lines in your text file. That seems fairly straightforward to me. Just casually looking at it, maybe you can check for "B8" in the start of a cell's text. If found, send a vbCrLf to your text file so that a new line is started.

    Alternatively, maybe you could search for the entire "B8:64:91:68:1C:50". That seems to be what you're using to define a new line. However, I've got no idea what this data relates to.

    Personally, it seems like a fairly straightforward parsing issue. You just need to thoroughly understand the structure of your data and how to appropriately parse it.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: How to introduce line break each 12 words when writing a file'?

    ams

    I think I get your drift .. here is my initial stab at it.

    Code:
            With FG1
                .Top = 500
                .Left = 500
                .Width = 9000 - 50
                .Height = 1400
                .Visible = True
                .Cols = 12
                .Rows = 5
                .FixedCols = 0
                For ii = 0 To 11
                    .ColWidth(ii) = 750
                Next ii
                ' 1a. read 1st line .. has CR .. carriage Return
                fpath = "D:\VBForums\amsf1.txt"
                Open fpath For Input As #1
                Line Input #1, xtr
                ' 1b. populate FG "headers"
                For ii = 1 To 12
                    nn = InStr(xtr, ";")
                    If nn > 0 Then
                        txt = Left(xtr, nn - 1)
                    Else
                        txt = xtr
                    End If
                    .TextMatrix(0, ii - 1) = txt
                    ' prep for next
                    xtr = Mid(xtr, nn + 1)
                Next ii
                ' 2a. read 2nd line .. 1 very long line of text, no CR's
                Line Input #1, xtr
                Close #1
                ' 2b. split at each ";"
                rr = 1
                For rr = 1 To 3
                    For cc = 1 To 12
                        nn = InStr(xtr, ";")
                        If nn > 0 Then
                            txt = Left(xtr, nn - 1)
                        Else
                            txt = xtr
                        End If
                        .TextMatrix(rr, cc - 1) = txt
                        .Col = cc - 1
                        .CellAlignment = 0
                        ' prep for next
                        xtr = Mid(xtr, nn + 1)
                        If cc = 12 Then
                            Exit For
                        End If
                    Next cc
                Next rr
            End With
    .. here is the textfile as I interpreted it

    Code:
    #MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#   
    ;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps-20MHz/1S;1131,1109;26m4s;210ms;-62@1Mbps;25   dB;99%;60958;192.168.100.253;CCK:1-11;;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps- 20MHz/1S;1207,1185;27m10s;440ms;-32@6Mbps;55 dB;96%;60488;192.168.100.253;CCK:1-   11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    Assumptions
    1. line 1 has a Carriage Return at the end if it
    2. line 2 does not .. it is one very long line of text


    .. and here is a screenshot

    Name:  amsF1.jpg
Views: 329
Size:  23.6 KB

    Issues
    1. line 1, used for col headers, seems ok
    2. the other lines do not
    3. I removed the (1st line), etc, text as I assumed it was for discussion purposes
    4. Each new row seems to be offset by 1 col.


    There seem to be some issues regarding location of your ";" separators
    Perhaps you could post the actual text file

    HTH

    Spoo

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to introduce line break each 12 words when writing a file'?

    Gotta ask a simple question. Why not just output the text file in a format easier for you to parse?

    Write to file, the record using whatever delimiter you want. And at the end of the record, write a carriage return. Now you can use Line Input to retrieve an entire record vs trying to parse out run-on record data. See my comments in the code, prefixed with <<<
    Code:
        For i = 1 To Grid1.Rows - 1
            Grid1.Row = i  'Empiezo en la fila 1 no en la cero
            For j = 1 To Grid1.Cols - 2 ' <<< note we changed this to 2
                Grid1.Col = j
                ' Write the data
                Print #Free_File, Grid1.Text & Delimitador;
            Next
            Grid1.Col = j  ' <<< write last field and a carriage return
            Print #Free_File, Grid1.Text & Delimitador ' <<< note no semicolon
        Next
    Edited: That final "Print" statement probably shouldn't include the Delimitador value.
    If using code like the above, you can use Line Input statements to re-populate your grid
    Code:
    Dim sFields() As String, sRecord As String, sHeader As String
    
    ... open the file
        Line Input #Free_File, sHeader
        Do Until EOF(Free_File)
            Line Input #Free_File, sRecord
            If sRecord = String(Len(sRecord), Delimitador) Then
                ' empty record
            ElseIf sRecord <> vbNullString Then ' ignore blank lines in text file
                ' populate your Grid
                sFields() = Split(sRecord, Delimitador)
                For j = 0 To UBound(sFields)
                    .... add field data to grid
                Next
            End If
        Loop
        Close #Free_File
    Last edited by LaVolpe; Oct 13th, 2017 at 08:26 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: How to introduce line break each 12 words when writing a file'?

    Quote Originally Posted by LaVolpe View Post
    Gotta ask a simple question. Why not just output the text file in a format easier for you to parse?
    Indeed.

    (Somewhat OT .. your boys did us in .. here's to hoping you go all the way)

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to introduce line break each 12 words when writing a file'?

    Quote Originally Posted by Spooman View Post
    your boys did us in .. here's to hoping you go all the way)
    That was a very fan-friendly game -- lots of plays to cheer & jeer for both teams' fans.
    P.S. Dusty seems to be a playoff jinx. We dealt with it when he was with the Cubs
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    39

    Re: How to introduce line break each 12 words when writing a file'?

    Thank you for your help.

    I explain you what I want with the next picture. I want to read my FlexGrid of two columns (I read only the second colum) and a variable number of rows because I am monitoring data and each time I click a button, a new group of data is added to the table.

    In the next picture I show you what I have:
    Name:  SCREENSHOT2.jpg
Views: 250
Size:  4.3 KB

    And in this picture I show you what I want:

    Name:  screenshot.jpg
Views: 319
Size:  7.0 KB


    The second MAC direction could be different from the first one.

  8. #8

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    39

    Re: How to introduce line break each 12 words when writing a file'?

    anybody knows?

  9. #9
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: How to introduce line break each 12 words when writing a file'?

    ams

    Sorry to say, but I don't understand what you mean
    Plus, your images are too small to read
    Did you try the code in my post #3?

    Spoo

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to introduce line break each 12 words when writing a file'?

    'Print #Free_File, ""
    this line commented out in your code should put a new line for each row, but no need to print anything
    Code:
    print #free_file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: How to introduce line break each 12 words when writing a file'?

    Pete

    Good catch

    Spoo

  12. #12

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    39

    Re: How to introduce line break each 12 words when writing a file'?

    Thank you for your help Spoo but the solution of #3 is not what I want.

    I have this grid:
    Name:  screenshot.PNG
Views: 282
Size:  7.7 KB

    As you can see, my application cathes the same data of various devices. In the next rows from "Dispositivo 2 (Device 2 in english)" there are the same data as you can se of "Dispositivo 1" but ovbiously with different values.

    I want to read all the grid ( but only the second column) and save the data in rows in a txt file (1 row for each device). Example: If the app detects two devices I want this in my txt file:

    #MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#
    B8:64:91:68:1C:50;65Mbps-20MHz/1S;5.5Mbps;36,44;22s;150ms;-35@HT20-7;51 dB;52%;3971;192.168.100.253;CCK:1-11;
    B8:64:91:68:1C:51;60Mbps-20MHz/1S;5.5Mbps;37,45;23s;460ms;-35@HT20-7;51 dB;54%;3971;192.168.100.253;CCK:1-11;

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to introduce line break each 12 words when writing a file'?

    try like
    Code:
    Grid1.column = 2
     For i = 1 To Grid1.Rows - 1 step 13
         j = 1 to 12
              grid1.row = i + j
              Print #Free_File, Grid1.text & Delimitador;
        next
        print #free_file
    next
    assumes that Dispositivo 1 is the text in row 1, this is untested
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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