Jump to content

Filewrite from Array glitch?


Recommended Posts

Morning (here anyway). I have a...glitch? Or maybe its a feature of the _FileWriteFromArray command.

My program reads and writes to a text file. It reads the file to the array fine and adds and deletes as needed perfect in array form, but as soon as it writes back to the text file...I get an unneeded line count and page break at the top of the file. See below:

CODE

5

2

Test1 Test1

Test2 Test2

Test3 Test3

Test4 Test4

That was after 2 runs. First being 2 entrys as was the next one (5 = entries plus the number 2). I would post the code here but if you run the _filewritefromarray example...it does the same thing....sooo...I guess is there a why to stop that...or anyone think of a failsafe to delete those?

Thanks.

"Could be worse. Could be raining."

Link to comment
Share on other sites

  • Moderators

Hi,

When you use _FileReadToArray, from the Helpfile:

$aArray[0] will contain the number of records read into the array.

When you resave the array using _FileWriteFromArray, there is a parameter which enables you to miss out this first element. From the HelpFile:

_FileWriteFromArray($File, $a_Array[, $i_Base = 0[, $i_UBound = 0]])

Parameters

$File String path of the file to write to, or a file handle returned from FileOpen().

$a_Array The array to be written to the file.

$i_Base Optional: Start Array index to read, normally set to 0 or 1. Default=0

$i_Ubound Optional: Set to the last record you want to write to the File. default=0 - whole array

So using the $i_Base parameter should solve your problem.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi,

When you use _FileReadToArray, from the Helpfile:

$aArray[0] will contain the number of records read into the array.

When you resave the array using _FileWriteFromArray, there is a parameter which enables you to miss out this first element. From the HelpFile:

_FileWriteFromArray($File, $a_Array[, $i_Base = 0[, $i_UBound = 0]])

Parameters

$File String path of the file to write to, or a file handle returned from FileOpen().

$a_Array The array to be written to the file.

$i_Base Optional: Start Array index to read, normally set to 0 or 1. Default=0

$i_Ubound Optional: Set to the last record you want to write to the File. default=0 - whole array

So using the $i_Base parameter should solve your problem.

M23

Awesome...I still can't find that in the helpfile but It fixed it...to a point. I now no longer have the number data which is probably the worse of the two but it is still inserting linebreaks.

CODE

Test1 TEST1

Test2 Test2

Test 3 TEST3

Test4 KILL

Seems like 4 breaks then the data...

"Could be worse. Could be raining."

Link to comment
Share on other sites

  • Moderators

Glad we managed to solve at least half of the problem.

Firstly, try using _ArrayDisplay (you have to include Array.au3) within your script just before using _FileWriteFromArray to check your array in memory is actually what you think it is. _FileWriteFromArray will write what is there, so make sure you have not added the line breaks yourself somewhere.

Secondly, please post the _FileWriteFromArray and _FileReadToArray code you are using in your script. Always a good idea to let someone else have a look at it.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Glad we managed to solve at least half of the problem.

Firstly, try using _ArrayDisplay (you have to include Array.au3) within your script just before using _FileWriteFromArray to check your array in memory is actually what you think it is. _FileWriteFromArray will write what is there, so make sure you have not added the line breaks yourself somewhere.

Secondly, please post the _FileWriteFromArray and _FileReadToArray code you are using in your script. Always a good idea to let someone else have a look at it.

M23

Yes to the ArrayDisplay and it contains the same line break but I'm not really sure where its coming from.

Below is actually the entire "program"...I'm too lazy to tweak it to hide what its for...

CODE
#include <File.au3>

#include <GUIConstants.au3>

#Include <Array.au3>

#Region ### START Koda GUI section ### Form=C:\Documents and Settings\dubinl\My Documents\cidplatv2.kxf

$Form2 = GUICreate("LCPD LPR", 413, 305, 303, 219)

GUISetIcon("D:\005.ico")

GUICtrlCreateTab(8, 8, 396, 256)

GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)

$TabSheet1 = GUICtrlCreateTabItem("Add Plate")

$plate = GUICtrlCreateInput("", 160, 68, 121, 22)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$Label1 = GUICtrlCreateLabel("License Plate #", 72, 68, 77, 18)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$Label2 = GUICtrlCreateLabel("State", 72, 108, 29, 18)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$Label3 = GUICtrlCreateLabel("Alarm Code", 72, 148, 60, 18)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$Label4 = GUICtrlCreateLabel("Alarm Notes", 72, 188, 63, 18)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$state = GUICtrlCreateCombo("", 160, 108, 50, 25)

GUICtrlSetData(-1, "PA|AL|AK|AZ|AR|CA|CO|CT|DE|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM

|NY|NC|ND|OH|OK|OR|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY")

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$alarm = GUICtrlCreateCombo("", 160, 148, 200, 25)

GUICtrlSetData(-1, "#1 STOLEN VEHICLE!|#2 WARRANT OR MISSING PERSON!|#3 STOLEN PLATE!|#4 SUSPENDED OR REVOKED REG!|#5 SCOFFLAW OR OTHER VIOLATION!")

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$notes = GUICtrlCreateInput("", 160, 188, 210, 22)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$addsubmit = GUICtrlCreateButton("Submit", 296, 224, 75, 25, 0)

$TabSheet2 = GUICtrlCreateTabItem("Remove Plate")

GUICtrlCreateInput("", 144, 127, 121, 22)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

$delsubmit = GUICtrlCreateButton("Remove", 168, 168, 75, 25, 0)

$platetodel = GUICtrlCreateLabel("License Plate #", 168, 103, 77, 18)

GUICtrlSetFont(-1, 8, 400, 0, "Arial")

GUICtrlCreateTabItem("")

$Finish = GUICtrlCreateButton("&Finished", 246, 272, 75, 25, 0)

$Cancel = GUICtrlCreateButton("&Cancel", 328, 272, 75, 25, 0)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

Dim $cidhot ;The array that will work as a temp file

GUISetState()

While 1

$msg = GUIGetMsg()

Select

Case $msg = $addsubmit

;Inserts new plates

If Not _FileReadToArray("\\testcidhot.txt",$cidhot) Then

MsgBox(4096,"Error", " Error reading hotlist to array. Error:" & @error)

Exit

EndIf

_ArrayAdd($cidhot, GUICtrlRead($plate) & " " & GUICtrlRead($state) & GUICtrlRead($alarm) & " " & GUICtrlRead($notes))

_ArrayDisplay ($cidhot, "")

_FileWriteFromArray("\\testcidhot.txt", $cidhot, 1) ;Dumps the array back into the text File

;Clear old data

GUICtrlSetData ($plate, "")

GUICtrlSetData ($notes, "")

Case $msg = $delsubmit

_FileReadToArray("testcidhot.txt", $cidhot) ;Dumps the current CIDhot list into a an array so it is workable

$Pos = _ArraySearch ($cidhot, $delsubmit, 0, 0, 0, True) ;Finds and returns the array position of the plate

_ArrayDelete($cidhot,$pos) ;Deletes the line in the array containing that plate

_FileWriteFromArray("\\testcidhot.txt", $cidhot, 1) ;Dumps the array back into the text File

GUICtrlSetData ($delsubmit, "")

Case $msg = $GUI_EVENT_CLOSE Or $msg = $Cancel

ExitLoop

Case $msg = $Finish

ExitLoop

EndSelect

WEnd

GUIDelete()

Exit

"Could be worse. Could be raining."

Link to comment
Share on other sites

Persosnally I don't see why you'd have the extra linebreaks right off but I can give a bit of advice. You should use Switch instead of Select, that way you only need Case $GUI_EVENT_CLOSE instead of Case $msg = $GUI_EVENT_CLOSE. Just makes cleaner code in my opinion.

Link to comment
Share on other sites

OK! I've kind of got....part of it. If you do

CODE
_FileWriteFromArray("\\testcidhot.txt", $cidhot, 2) NOTE THE 2

You only get 1 line break at the very start of the file...slowly working our way towards no breaks!

Edited by Imppilot

"Could be worse. Could be raining."

Link to comment
Share on other sites

  • Moderators

There were a couple of problems in the code, but this version works correctly:

#include <File.au3>
#include <GUIConstants.au3>
#Include <Array.au3>

#Region ### START Koda GUI section ### Form=C:\Documents and Settings\dubinl\My Documents\cidplatv2.kxf
$Form2 = GUICreate("LCPD LPR", 413, 305, 303, 219)
GUISetIcon("D:\005.ico")
GUICtrlCreateTab(8, 8, 396, 256)
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
$TabSheet1 = GUICtrlCreateTabItem("Add Plate")
$plate = GUICtrlCreateInput("", 160, 68, 121, 22)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Label1 = GUICtrlCreateLabel("License Plate #", 72, 68, 77, 18)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Label2 = GUICtrlCreateLabel("State", 72, 108, 29, 18)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Label3 = GUICtrlCreateLabel("Alarm Code", 72, 148, 60, 18)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Label4 = GUICtrlCreateLabel("Alarm Notes", 72, 188, 63, 18)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$state = GUICtrlCreateCombo("", 160, 108, 50, 25)
GUICtrlSetData(-1, "PA|AL|AK|AZ|AR|CA|CO|CT|DE|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|

NY|NC|ND|OH|OK|OR|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY")
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$alarm = GUICtrlCreateCombo("", 160, 148, 200, 25)
GUICtrlSetData(-1, "#1 STOLEN VEHICLE!|#2 WARRANT OR MISSING PERSON!|#3 STOLEN PLATE!|#4 SUSPENDED OR REVOKED REG!|#5 SCOFFLAW OR OTHER VIOLATION!")
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$notes = GUICtrlCreateInput("", 160, 188, 210, 22)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$addsubmit = GUICtrlCreateButton("Submit", 296, 224, 75, 25, 0)
$TabSheet2 = GUICtrlCreateTabItem("Remove Plate")
$platetodel = GUICtrlCreateInput("", 144, 127, 121, 22) ; correct place
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$delsubmit = GUICtrlCreateButton("Remove", 168, 168, 75, 25, 0)
GUICtrlCreateLabel("License Plate #", 168, 103, 77, 18)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
GUICtrlCreateTabItem("")
$Finish = GUICtrlCreateButton("&Finished", 246, 272, 75, 25, 0)
$Cancel = GUICtrlCreateButton("&Cancel", 328, 272, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Dim $cidhot;The array that will work as a temp file 

GUISetState()

$test = _FileReadToArray(@ScriptDir & "\testcidhot.txt", $cidhot);Dumps the current CIDhot list into a an array so it is workable
If Not $test Then
    MsgBox(4096,"Error", " Error reading hotlist to array. Error:" & @error)
    Exit
EndIf 

_ArrayDisplay ($cidhot, "On Loading")

While 1
    
    $msg = GUIGetMsg()
    
    Select
        Case $msg = $addsubmit
        ;Inserts new plates
            _ArrayAdd($cidhot, GUICtrlRead($plate) & " " & GUICtrlRead($state) & GUICtrlRead($alarm) & " " & GUICtrlRead($notes))
            _ArrayDisplay ($cidhot, "After Insertion")
        ;Clear old data
            GUICtrlSetData ($plate, "")
            GUICtrlSetData ($notes, "")
            
        Case $msg = $delsubmit
            $search = GUICtrlRead($platetodel)
            $Pos = _ArraySearch ($cidhot, $search, 0, 0, 0, 1);Finds and returns the array position of the plate
            If $Pos <> -1 Then
                _ArrayDelete($cidhot,$pos);Deletes the line in the array containing that plate
            Else
                MsgBox(4096,"Error", "Cannot find plate:" & @error)
                ExitLoop
            EndIf
            _ArrayDisplay ($cidhot, "After Deletion")
            GUICtrlSetData ($platetodel, "");($delsubmit, "")
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $Cancel
            ExitLoop
        Case $msg = $Finish
            ExitLoop
    EndSelect
WEnd


_ArrayDisplay ($cidhot, "On Closing")
_FileWriteFromArray(@ScriptDir & "\testcidhot.txt", $cidhot, 1);Dumps the array back into the text File

GUIDelete()

Exit

The problems were:

1. You had the $platetodel handle pointed at the delete button and not the plate inputbox - and that's why the button went blank and not the box!

2. You were using this handle to search the array, instead of using the contents of the input box. Thus the plate number was not getting passed to the array which was screwing up the search. And as there was no errorchecking, a returned $pos of "-1" from any error (such as not finding the plate) deleted the first element of the array regardless of its contents.

I also changed the read and write to act only once, at the beginning and end of the code. If your machine is particularly prone to falling over (and given the reason for the script, that might well be the case!), you may want to return to the old method and accept the overhead.

I have left the ArrayDisplay lines in so you can check - on my machine it works perfectly. I also changed the index file location to the script folder so I knew where it was for checking. You can reset this to what you require.

I expect to be let off any tickets next time I'm in PA!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...