Jump to content

Search the Community

Showing results for tags 'file writing'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • General
    • Announcements and Site News
    • Administration
  • AutoIt v3
    • AutoIt Help and Support
    • AutoIt Technical Discussion
    • AutoIt Example Scripts
  • Scripting and Development
    • Developer General Discussion
    • Language Specific Discussion
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Categories

  • AutoIt Team
    • Beta
    • MVP
  • AutoIt
    • Automation
    • Databases and web connections
    • Data compression
    • Encryption and hash
    • Games
    • GUI Additions
    • Hardware
    • Information gathering
    • Internet protocol suite
    • Maths
    • Media
    • PDF
    • Security
    • Social Media and other Website API
    • Windows
  • Scripting and Development
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Location


WWW


Interests

Found 1 result

  1. Hello together, I need to write just one line into a file. The file is generated by an other program and contains some information first and afterwards a PDF-file. FileGetEncoding results in 16 (Binary), and it containes @CRLF, @CR and @LF, which need to stay right where they are. The best results I got so far were _FileWriteToLine, which replaced every @LF and @CR with @CRLF and destroyed the PDF inside of my file. So the line was added but the file got unusable. I read the following thread and made my own example to fit my problem, but they doesn't seem to work: $sFileName = "test.txt" prepareFile($sFileName) ;Local $result = FileWriteAtLine($sFileName,"Add in line 3",3) Local $result = FileWriteAfter($sFileName,"Add in line 3","line3") If $result = 1 Then ConsoleWrite("That seems to work." & @CRLF) Else ConsoleWrite("That didn't work. Read lines: " & -$result & @CRLF) EndIf Exit Func prepareFile($sFileName) FileWrite($sFileName, "") $hFile = FileOpen($sFileName,2+16) FileWrite($hFile,"line1" & @CRLF & "line2" & @CRLF & "line3" & @CRLF & "line4" & @CRLF & "line5" & @CRLF & "etc" & @LF & "etc" & @CR & "etc" & @CR & "etc" & @LF & "etc" & @CRLF) FileFlush($hFile) FileClose($hFile) EndFunc Func FileWriteAtLine($sFileName,$sLine,$iLine) $linecount = 0 Local $iEncoding = FileGetEncoding($sFileName,2) ConsoleWrite("Encoding: " &$iEncoding & @CRLF ) $iEncoding = 16 Local $sTempName = "tempfile.tmp" $file = FileOpen($sFileName, $iEncoding) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf Local $sBuffer = FileRead($file,FileGetSize($sFileName)) If @error = -1 Then ConsoleWrite("CANNOT READ" & @CRLF) FileClose($file) ConsoleWrite($sBuffer) ; Read in lines of text until the EOF is reached FileWrite($sTempName,"") Local $hTempfile = FileOpen($sTempName,1 + $iEncoding) $iResult = 1 While 1 Local $iZeichen = StringInStr($sBuffer, @CRLF) If $iZeichen > 0 Then $linecount += 1 If $linecount = $iLine Then FileWrite($hTempfile,$sLine) FileWrite($hTempfile,$sBuffer);Write everything else ExitLoop EndIf Local $line = StringLeft($sBuffer,$iZeichen + 1) $sBuffer = StringTrimLeft($sBuffer, $iZeichen +1) FileWrite($hTempfile,$line) ;FileFlush($hTempfile) Else FileWrite($hTempfile,$sBuffer);Write everything else $iResult = -$linecount ExitLoop EndIf Wend FileClose($hTempfile) FileMove($sTempName,$sFileName,1) Return $iResult EndFunc Func FileWriteAfter($sFileName,$sLine,$sAfter) $linecount = 0 Local $iEncoding = FileGetEncoding($sFileName,2) ConsoleWrite("Encoding: " &$iEncoding & @CRLF ) $iEncoding = 16 Local $sTempName = "tempfile.tmp" $file = FileOpen($sFileName, $iEncoding) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf Local $sBuffer = FileRead($file,FileGetSize($sFileName)) If @error = -1 Then ConsoleWrite("CANNOT READ" & @CRLF) FileClose($file) ConsoleWrite($sBuffer) ; Read in lines of text until the EOF is reached FileWrite($sTempName,"") Local $hTempfile = FileOpen($sTempName,1 + $iEncoding) $iResult = 1 Local $iZeichen = StringInStr($sBuffer,$sAfter) If $iZeichen > 0 Then Local $sFirst = StringLeft($sBuffer,$iZeichen + StringLen($sAfter)) $sBuffer = StringTrimLeft($sBuffer, $iZeichen + StringLen($sAfter)) FileWrite($hTempfile,$sFirst) FileWrite($hTempfile,$sLine) FileWrite($hTempfile,$sBuffer) Else FileWrite($hTempfile,$sBuffer);Write everything else $iResult = $linecount EndIf FileClose($hTempfile) FileMove($sTempName,$sFileName,1) Return $iResult EndFunc Both function result in 0, so my string or the @CRLF cannot be found inside of the buffer. What's my mistake? Thanks, Omega19
×
×
  • Create New...