Jump to content
Sign in to follow this  
dobbelina

FileWriteToLine Problem

Recommended Posts

dobbelina

Hi !

Example to illustrate the problem:

The script is searching for the word "dog".

If "dog" is not found it will look for the word "cat" and

add "dog" beneath it.

If "dog" is found the script wont execute the function,

so to safeguard against multiple writes.

The script does everything perfectly except for one thing,

It deletes the word underneath where "dog" is inserted.

Therefore i need a linefeed or something, so i can keep

the "fish" (See example)

#Include <File.au3>
Global $success = False
$myfile = "C:\Test.txt"
$file = FileRead($myfile, FileGetSize($myfile))
$file = StringSplit($file, @CRLF, 1)
for $i = 0 to $file[0] Step 1
    If StringInStr($file[$i], "dog") Then
    $success = True
         ExitLoop
    EndIf
Next
if $success = Not True Then
   MsgBox(0, "Not fund", "Line not found, so lets write")
    _test()
EndIf
Func _test()
for $i = 0 to $file[0] Step 1
    If StringInStr($file[$i], "cat") Then

           _FileWriteToLine($myfile, $i+1,"dog", 1)
      EndIf
  Next
  EndFunc

Original Test.txt:

monkey

bird

cat

fish

crocodile

Becomes: (fish is gone)

monkey

bird

cat

dog

crocodile

But i want:

monkey

bird

cat

dog

fish

crocodile

It's cracking me up this problem, any help is much appreciated !

Share this post


Link to post
Share on other sites
jchd

Like this:

#Include <Array.au3>

Local Const $myfile = "Test.txt"        ;; I had to change that
Local Const $cat = "cat"
Local Const $dog = "dog"
Local $lines

_FileReadToArray($myfile, $lines)
Local $index = _ArraySearch($lines, $cat, 1)
If $index = -1 Then $index = UBound($lines) - 1
_ArrayInsert($lines, $index + 1, $dog)
_FileWriteFromArray($myfile, $lines, 1)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
hawky358

The _Filewritetoline() function already has this built in :idea:

Here's the syntax:

_FileWriteToLine($sFile, $iLine, $sText[, $fOverWrite = 0])

change _FileWriteToLine($myfile, $i+1,"dog", 1)

to _FileWriteToLine($myfile, $i+1,"dog", 0)

or _FileWriteToLine($myfile, $i+1,"dog")

Edited by hawky358

Share this post


Link to post
Share on other sites
dobbelina

Ahh, thank you very much! :idea:

Now it works as i want.

Share this post


Link to post
Share on other sites
Sritra

Ok, what am i doing wrong?

I just wanted to check if my _filewritetoline works, but it doesn't seem so

#Include 
Global $success = False
$myfile = "C:\Users\Fabi\Desktop\elements\test.txt"
$file = FileRead($myfile, FileGetSize($myfile))
$file = StringSplit($file, @CRLF, 1)
for $i = 0 to $file[0] Step 1
If StringInStr($file[$i], "dog") Then
$success = True
ExitLoop
EndIf
Next
If $success = Not True Then
MsgBox(0, "Not fund", "Line not found, so lets write")
_test()
EndIf
Func _test()
for $i = 0 to $file[0] Step 1
If StringInStr($file[$i], "cat") Then
_FileWriteToLine($myfile, $i+1,"dog",0)
EndIf
Next
EndFunc

It shouts this Error:

>"C:Program Files (x86)AutoIt3SciTE..autoit3.exe" /ErrorStdOut "C:UsersFabiDesktopelementstest.au3"

>Exit code: 0 Time: 0.785

And the test.txt hasn't change :think:

Share this post


Link to post
Share on other sites
Melba23

Sritra,

Your code works fine for me, but you have a major logical flaw within it. You are adding a line to the file when you write to it - that means that your line count for any subsequent _FileWriteToLine will be incorrect. If we start with this file:

cow
pig
cat
rat
fly

we get this aftet one replacement:

cow
pig
cat
dog
rat
fly

So you can see that all the subsequent lines are now pushed down one and so you will not write the next "dog" in the correct place. :(

You get over this by working up from the bottom of the list so that all the changes are below the current position in the file. I have rewritten the script to show this working:

#include <File.au3>

Global $fSuccess = False, $aLines

; This gets the file directly into an array - much faster than what you had before
$sMyFile = "test.txt"
_FileReadToArray($sMyFile, $aLines)

For $i = 0 To $aLines[0]
    If StringInStr($aLines[$i], "dog") Then
        $fSuccess = True
        ExitLoop
    EndIf
Next

If Not $fSuccess Then
    MsgBox(0, "Not found", "Line not found, so lets write")
    _test()
Else
    MsgBox(0, "Found", "OK")
EndIf

Func _test()
    For $i = $aLines[0] To 1 Step -1
        If StringInStr($aLines[$i], "cat") Then
            _FileWriteToLine($sMyFile, $i + 1, "dog", 0)

            ; This is just to show you what is happening as we move up the array
            $sFile = FileRead($sMyFile)
            MsgBox(0, "Current file content", $sFile)

        EndIf
    Next
EndFunc   ;==>_test

All clear? Please ask if not. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Sritra

ok, I know my fault...

i changed the File.au3..... :lmao:

Share this post


Link to post
Share on other sites
Melba23

Sritra,

And what about the logic problem I highlighted? Will it be worth our responding to you in the future or will any help offered simply be ignored? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Sritra

I thank You for that, but i just wanted to see, if my _FileWriteToLine() is working, the result was uneccesary, but thanks for trying to help me :thumbsup:

Share this post


Link to post
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
Sign in to follow this  

×