Jump to content

Help with writing to txt file please!


Illuma
 Share

Recommended Posts

Hi, I am struggling to save a string to a file, I am sure there is a simple way to do it but I cant find it anywhere!

What I have is a file called test.txt in the file the first line has 055, the next line has 2008 and the third line is 0.

What I am doing is using the file as a variable store, my program reads from the file lines 1 and 2 fine, as they are used as read only by the program. The third line is read in to the program to be used as a count then it is incremented by 1 (when a button on the keyboard is pressed by using hotkeyset command), where my problems start is I need to be able to write the new count value back over the old value one line 3 of the test.txt file and I cant seem to find a way of doing it? I need this so when the program is stopped and re-started I can continue the count from where I left off.

Any help would be greatly appreciated.

Link to comment
Share on other sites

Just so you know I have spent hours trying different ways of doing what I want to do, I have searched forum and Google. Obviously we are not all as smart as you! That was my first post yet I have written several programs and I have never needed to ask for help, up to this point I have found all I need by searching and trying.

The function you mentioned never even came up in any of my attempts to search, only when I had the full function name was I able to find it in help files and only one reference on the net!

Anyway I have tried it and I can only get it to work with text in commas not a variable, this is the line of code I am using

_FileWriteToLine("test.txt", 3, $newcount, 1)

Works fine if I use "123" in the place of $newcount but not when I use $newcount!

As I am new to AutoIt & programming I would and do appreciate help, as in my line of work I help people all the time without sarcasm so it P****S me of to get snide comments thrown back with no justification!

Edited by Illuma
Link to comment
Share on other sites

  • Developers

As I am new to AutoIt & programming I would and do appreciate help, as in my line of work I help people all the time without sarcasm so it P****S me of to get snide comments thrown back with no justification!

Listen, This forum here is a hobby for us all and there is no need to take this too personal when we have some mild "playing" here.

You will find you get a lot of help from regulars when the questions are valid/serious and do not come across as coming for impatient gamers/hacker kiddies. (.. and before you take this wrongly: I do not imply this is you.)

Now to your issue: Show use a little code snippet where writing the variable doesn't work so we can see what you are doing wrong.

Jos ;)

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

How are you setting the value of $newcount before the _FileWriteToLine()? Post a short reproducer script that reproduces your symptoms.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

How are you setting the value of $newcount before the _FileWriteToLine()? Post a short reproducer script that reproduces your symptoms.

;)

Ok, thanks for helping. I have attached the code using attachments if thats correct. If not I will post again when I work it out!

text in test.txt is 055 on line 1 2008 on line 2 and currently 998 on line 3.code.txt

Edited by Illuma
Link to comment
Share on other sites

Hi, I am struggling to save a string to a file, I am sure there is a simple way to do it but I cant find it anywhere!

What I have is a file called test.txt in the file the first line has 055, the next line has 2008 and the third line is 0.

What I am doing is using the file as a variable store, my program reads from the file lines 1 and 2 fine, as they are used as read only by the program. The third line is read in to the program to be used as a count then it is incremented by 1 (when a button on the keyboard is pressed by using hotkeyset command), where my problems start is I need to be able to write the new count value back over the old value one line 3 of the test.txt file and I cant seem to find a way of doing it? I need this so when the program is stopped and re-started I can continue the count from where I left off.

Any help would be greatly appreciated.

;//Compile any Fetched Report [User Specific - Stop Outlook Locking Source File]
Func _ProcessorPushedReports($sReportPath, $sReportID, $sPushAddr, $sFooter)
    ;//Calculate the RecordID
    _RecordIdProcessor(0, 0)
    ;//File Path
    $sUserRecRptID = $sLogDir & (@MON & StringRight(@YEAR, 2)) & "\Day\" & @MDAY & "\" & $RecordId & $sReportId & ".txt"
    Local $sUserRptCounter
    While 1
        Local $sUserReportRec, $sUserRecWrite
        ;//Exit if No File Found
        If Not FileExists($sReportPath) Then ExitLoop
        ;//Open the File
        $UserRptFile = FileOpen($sUserRecRptID, 9)
        ;//Read the File to an Array
        _FileReadToArray($sReportPath, $sUserReportRec)
        For $x = 1 To $sUserReportRec[0]
            $uRecRptID = ""
            $sUserRecWrite = ""
            $sUserRptCounter += 1
            $sUserRecWrite = $sUserReportRec[$x]
            ;//Write the Record to the End of Line
            FileWriteLine($UserRptFile, $sUserRecWrite)
        Next
        ;//Add the Footer Record
        If $sFooter = 1 Then
            FileWriteLine($UserRptFile, "")
            FileWriteLine($UserRptFile, "Total Records " & $sUserRptCounter)
            FileWriteLine($UserRptFile, StringStripWS($Title, 8) & " - " & @ComputerName)
            FileWriteLine($UserRptFile, "[" & $sTime & "]")
        EndIf
        FileWriteLine($UserRptFile, "")
        FileWriteLine($UserRptFile, "Report PUSHED to: " & $sPushAddr)
        FileWriteLine($UserRptFile, "")
        FileClose($UserRptFile)
        FileSetAttrib($sUserRecRptID, "+RS")
        ExitLoop
    WEnd
    $s_Attachment = $sUserRecRptID
EndFunc   ;==>_ProcessorFetchedReports
Link to comment
Share on other sites

  • Developers

The _FileWriteToLine() returned rc = 0 and @error = 6 (6 = $sText is invalid)

This version works:

#include <File.au3>
HotKeySet("1","run_one")

func run_one()
    $oldcount = FileReadLine("test.txt", 3)
    $result = StringCompare($oldcount, "999")
    if $result = 0 Then
        $newcount = 0
    Else
        $newcount = $oldcount +1
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newcount = ' & $newcount & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    EndIf
    $rc = _FileWriteToLine("test.txt", 3, string($newcount), 1)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $rc = ' & $rc & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
EndFunc


While 1
    Sleep(100000)
WEnd
Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

It's as designed, but I would consider that a bug. Do it this way to see the problem:

$RET = _FileWriteToLine($sFile, 3, 123, 1)
    MsgBox(0, "test", "$newcount = " & $newcount & @CRLF & "$RET = " & $RET & @CRLF & "@error = " & @error)

The UDF function for _FileWriteToLine() validates the input text with this:

Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
    If $iLine <= 0 Then Return SetError(4, 0, 0)
    If Not IsString($sText) Then Return SetError(6, 0, 0)
    
    ; ...
    
EndFunc

That is absurdly strict in the AutoIt environment. Unlike some other languages, you should be able to pass other variable types (like integers, floats, and handles, not objects or arrays) for text in a file write.

;)

Edit: Forgot to post a fix: I would changes that part of the UDF to this:

Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
    If $iLine <= 0 Then Return SetError(4, 0, 0)
    If Not IsString($sText) Then 
        $sText = String($sText)
        If $sText = "" Then Return SetError(6, 0, 0)
    EndIf
    
    ; ...
    
EndFunc

:evil:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The _FileWriteToLine() returned rc = 0 and @error = 6 (6 = $sText is invalid)

This version works:

#include <File.au3>
HotKeySet("1","run_one")

func run_one()
    $oldcount = FileReadLine("test.txt", 3)
    $result = StringCompare($oldcount, "999")
    if $result = 0 Then
        $newcount = 0
    Else
        $newcount = $oldcount +1
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newcount = ' & $newcount & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    EndIf
    $rc = _FileWriteToLine("test.txt", 3, string($newcount), 1)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $rc = ' & $rc & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
EndFunc


While 1
    Sleep(100000)
WEnd

Thanks for the help, working fine! I would have never tried that but you live and learn! also the consolewrite is cool could save me using 10,000 msgbox commands to debug!

Thanks again!

Link to comment
Share on other sites

  • Developers

Thanks for the help, working fine! I would have never tried that but you live and learn! also the consolewrite is cool could save me using 10,000 msgbox commands to debug!

Thanks again!

When you use the full SciTE4AutoIt3 version you have the option to add these debuglines by simply hitting Alt+D.

That is great for debugging your code. You can easily comment then out or remove them too just look in the helpfile Crtl+F1 or under Tools for directions.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I really don't like that behavior in _FileWriteToLine(). Automatic re-typing of input text is general feature of almost all AutoIt functions.

Opened BugTrac #1353.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

When you use the full SciTE4AutoIt3 version you have the option to add these debuglines by simply hitting Alt+D.

That is great for debugging your code. You can easily comment then out or remove them too just look in the helpfile Crtl+F1 or under Tools for directions.

Jos

I really don't like that behavior in _FileWriteToLine(). Automatic re-typing of input text is general feature of almost all AutoIt functions.

Opened BugTrac #1353.

;)

Even more useful info, keep it coming!

My first post hopefully proved I am not an impatient gamer/hacker or kiddie (just a small digg at Jos) and I learned some new things.

Link to comment
Share on other sites

  • Developers

My first post hopefully proved I am not an impatient gamer/hacker or kiddie (just a small digg at Jos) and I learned some new things.

Still deciding the proper category ;)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

I really don't like that behavior in _FileWriteToLine(). Automatic re-typing of input text is general feature of almost all AutoIt functions.

Opened BugTrac #1353.

;)

Agree ... I was surprised too that writing the number didn't work.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

My first post hopefully proved I am not an impatient gamer/hacker or kiddie...

Remains to be seen... there will still be a quiz on the contents of the Help File on Monday after the Christmas Break!

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 4 weeks later...

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...