Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Autoit 读取文件时如何确定换行符_Autoit - Fatal编程技术网

Autoit 读取文件时如何确定换行符

Autoit 读取文件时如何确定换行符,autoit,Autoit,我有一个AutoIt脚本,大部分都能正常工作。读取文件,写出所需内容,但不保留原始换行符。如果我读取UNIX格式文件(仅限LF),它将写出Windows格式文件(CR和LF) 除了切换到更健壮的东西,比如Python,我如何在AutoIt中解决这个问题 Opt("MustDeclareVars", 1) ;0 = no, 1 = require pre-declare #include <File.au3> #include <Array.au3> Local $

我有一个AutoIt脚本,大部分都能正常工作。读取文件,写出所需内容,但不保留原始换行符。如果我读取UNIX格式文件(仅限LF),它将写出Windows格式文件(CR和LF)

除了切换到更健壮的东西,比如Python,我如何在AutoIt中解决这个问题

Opt("MustDeclareVars", 1)   ;0 = no, 1 = require pre-declare

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

Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & @ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend

If FileExists($gInPath) Then
Else
    MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & @CRLF & $gInPath)
    Exit
EndIf

_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)

If $NumberOfLines >= 1000000 Then
    $FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
    $FileStringAppend = $NumberOfLines / 1000 & "K"
Else
    $FileStringAppend = $NumberOfLines
EndIf

$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
    MsgBox(4096, $gMsgBoxTitle, "File already exists" & @CRLF & $gOutPath)
    Exit
EndIf

$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)

; Check if file opened for reading OK
If $oFileIn = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & @CRLF & $gInPath)
    Exit
EndIf

; Check if file opened for writing OK
If $oFileOut = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & @CRLF & $gOutPath)
    Exit
EndIf

; Read in lines of text until the EOF is reached
$LineCount = 0
While 1
    $InLine = FileReadLine($oFileIn)
    $LineCount += 1
    If @error = -1 Then ExitLoop
    If $LineCount > $NumberOfLines Then ExitLoop
    FileWriteLine($oFileOut, $InLine & @CRLF)
WEnd

FileClose($oFileIn)
FileClose($oFileOut)
Opt(“MustDeclareVars”,1);0=否,1=需要预先声明
#包括
#包括
本地$gInPath=$CmdLine[1]
本地$NumberOfLines=$CmdLine[2]
本地$gInDrive、$gInDir、$gInFName、$gInExt、$gOutPath
Local$gMsgBoxTitle=“出现错误”&@ScriptName
本地$InLine
本地$LineCount
莱因当地元
本地$oFileOut
本地$FileStringAppend
如果文件存在($gInPath),则
其他的
MsgBox(4096,$gMsgBoxTitle,“此文件不存在”&@CRLF&$gInPath)
出口
恩迪夫
_路径拆分($gInPath、$gInDrive、$gInDir、$gInFName、$gInExt)
如果$NumberOfLines>=1000000,则
$FileStringAppend=$NumberOfLines/1000000&“M”
如果$NumberOfLines>=1000,则
$FileStringAppend=$NumberOfLines/1000&“K”
其他的
$FileStringAppend=$NumberOfLines
恩迪夫
$gOutPath=\u路径生成($gInDrive、$gInDir、$gInFName&“\u”&$FileStringAppend、$gInExt)
如果存在文件($gOutPath),则
MsgBox(4096$gMsgBoxTitle,“文件已存在”&@CRLF&$gOutPath)
出口
恩迪夫
$oFileIn=FileOpen($gInPath,0)
$oFileOut=FileOpen($gOutPath,1)
; 检查文件是否已打开以便读取OK
若$oFileIn=-1,则
MsgBox(4096,$gMsgBoxTitle,“无法打开文件进行读取”&@CRLF&$gInPath)
出口
恩迪夫
; 检查文件是否已打开以便写入OK
如果$oFileOut=-1,则
MsgBox(4096,$gMsgBoxTitle,“无法打开文件进行写入。”&@CRLF&$gOutPath)
出口
恩迪夫
; 按行阅读文本,直到达到EOF
$LineCount=0
而1
$InLine=FileReadLine($oFileIn)
$LineCount+=1
如果@error=-1,则退出
如果$LineCount>$NumberOfLines,则退出Loop
FileWriteLine($oFileOut、$InLine&@CRLF)
温德
文件关闭($oFileIn)
文件关闭($oFileOut)

查看此链接中的函数文档-。似乎您可以在
文件写入线
命令中关闭
&@CRLF

AutoIt应使用读入的同一行终止符,或

“如果该行不以@CR或@LF结尾,则为DOS换行符(@CRLF) 将自动添加。”


查看此链接中的函数文档-。似乎您可以在
文件写入线
命令中关闭
&@CRLF

AutoIt应使用读入的同一行终止符,或

“如果该行不以@CR或@LF结尾,则为DOS换行符(@CRLF) 将自动添加。”


这是我想出的解决办法。它能用,但我不确定它是最干净的

Opt("MustDeclareVars", 1)   ;0 = no, 1 = require pre-declare

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

Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & @ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend
Local Const $CHAR_READ_BLOCK = 100
Local $CharsRead = 0
Local $CrFound = 0
Local $LfFound = 0
Local $Newline
Local $InBlock

If FileExists($gInPath) Then
Else
    MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & @CRLF & $gInPath)
    Exit
EndIf

_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)

If $NumberOfLines >= 1000000 Then
    $FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
    $FileStringAppend = $NumberOfLines / 1000 & "K"
Else
    $FileStringAppend = $NumberOfLines
EndIf

$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
    MsgBox(4096, $gMsgBoxTitle, "File already exists" & @CRLF & $gOutPath)
    Exit
EndIf

$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)

; Check if file opened for reading OK
If $oFileIn = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & @CRLF & $gInPath)
    Exit
EndIf

; Check if file opened for writing OK
If $oFileOut = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & @CRLF & $gOutPath)
    Exit
EndIf

While $CrFound = 0 And $LfFound = 0
    $CharsRead += $CHAR_READ_BLOCK
    $InBlock = FileRead($oFileIn, $CharsRead)
    If StringRight($InBlock, 1) = @CR Then
        $InBlock = $InBlock & FileRead($oFileIn, $CharsRead)
    EndIf

    $CrFound = StringInStr($InBlock, @CR)
    $LfFound = StringInStr($InBlock, @LF)

    If $CrFound > 0 And $LfFound > 0 Then
        $Newline = @CRLF
    ElseIf $CrFound > 0 Then
        $Newline = @CR
    Else
        $Newline = @LF
    EndIf
WEnd

; Read first line of text
$InLine = FileReadLine($oFileIn, 1)
$LineCount = 1
FileWriteLine($oFileOut, $InLine & $Newline)

; Read in lines of text until the EOF is reached
While 1
    $InLine = FileReadLine($oFileIn)
    $LineCount += 1
    If @error = -1 Then ExitLoop
    If $LineCount > $NumberOfLines Then ExitLoop
    FileWriteLine($oFileOut, $InLine & $Newline)
WEnd

FileClose($oFileIn)
FileClose($oFileOut)
Opt(“MustDeclareVars”,1);0=否,1=需要预先声明
#包括
#包括
本地$gInPath=$CmdLine[1]
本地$NumberOfLines=$CmdLine[2]
本地$gInDrive、$gInDir、$gInFName、$gInExt、$gOutPath
Local$gMsgBoxTitle=“出现错误”&@ScriptName
本地$InLine
本地$LineCount
莱因当地元
本地$oFileOut
本地$FileStringAppend
本地常量$CHAR\u READ\u块=100
本地$CharsRead=0
本地$CRFOND=0
本地$LfFound=0
本地$Newline
本地$InBlock
如果文件存在($gInPath),则
其他的
MsgBox(4096,$gMsgBoxTitle,“此文件不存在”&@CRLF&$gInPath)
出口
恩迪夫
_路径拆分($gInPath、$gInDrive、$gInDir、$gInFName、$gInExt)
如果$NumberOfLines>=1000000,则
$FileStringAppend=$NumberOfLines/1000000&“M”
如果$NumberOfLines>=1000,则
$FileStringAppend=$NumberOfLines/1000&“K”
其他的
$FileStringAppend=$NumberOfLines
恩迪夫
$gOutPath=\u路径生成($gInDrive、$gInDir、$gInFName&“\u”&$FileStringAppend、$gInExt)
如果存在文件($gOutPath),则
MsgBox(4096$gMsgBoxTitle,“文件已存在”&@CRLF&$gOutPath)
出口
恩迪夫
$oFileIn=FileOpen($gInPath,0)
$oFileOut=FileOpen($gOutPath,1)
; 检查文件是否已打开以便读取OK
若$oFileIn=-1,则
MsgBox(4096,$gMsgBoxTitle,“无法打开文件进行读取”&@CRLF&$gInPath)
出口
恩迪夫
; 检查文件是否已打开以便写入OK
如果$oFileOut=-1,则
MsgBox(4096,$gMsgBoxTitle,“无法打开文件进行写入。”&@CRLF&$gOutPath)
出口
恩迪夫
而$CRFOND=0和$LFFOND=0
$CharsRead+=$CHAR\u READ\u块
$InBlock=FileRead($oFileIn,$CharsRead)
如果StringRight($InBlock,1)=@CR,则
$InBlock=$InBlock&FileRead($oFileIn,$CharsRead)
恩迪夫
$CrFound=StringInStr($InBlock,@CR)
$LfFound=StringInStr($InBlock,@LF)
如果$CRFOND>0和$LFFOND>0,则
$Newline=@CRLF
如果$CrFound>0,则
$Newline=@CR
其他的
$Newline=@LF
恩迪夫
温德
; 阅读第一行文字
$InLine=FileReadLine($oFileIn,1)
$LineCount=1
FileWriteLine($oFileOut、$InLine和$Newline)
; 按行阅读文本,直到达到EOF
而1
$InLine=FileReadLine($oFileIn)
$LineCount+=1
如果@error=-1,则退出
如果$LineCount>$NumberOfLines,则退出Loop
FileWriteLine($oFileOut、$InLine和$Newline)
温德
文件关闭($oFileIn)
文件关闭($oFileOut)

这是我提出的解决方案。它能用,但我不确定它是最干净的

Opt("MustDeclareVars", 1)   ;0 = no, 1 = require pre-declare

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

Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & @ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend
Local Const $CHAR_READ_BLOCK = 100
Local $CharsRead = 0
Local $CrFound = 0
Local $LfFound = 0
Local $Newline
Local $InBlock

If FileExists($gInPath) Then
Else
    MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & @CRLF & $gInPath)
    Exit
EndIf

_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)

If $NumberOfLines >= 1000000 Then
    $FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
    $FileStringAppend = $NumberOfLines / 1000 & "K"
Else
    $FileStringAppend = $NumberOfLines
EndIf

$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
    MsgBox(4096, $gMsgBoxTitle, "File already exists" & @CRLF & $gOutPath)
    Exit
EndIf

$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)

; Check if file opened for reading OK
If $oFileIn = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & @CRLF & $gInPath)
    Exit
EndIf

; Check if file opened for writing OK
If $oFileOut = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & @CRLF & $gOutPath)
    Exit
EndIf

While $CrFound = 0 And $LfFound = 0
    $CharsRead += $CHAR_READ_BLOCK
    $InBlock = FileRead($oFileIn, $CharsRead)
    If StringRight($InBlock, 1) = @CR Then
        $InBlock = $InBlock & FileRead($oFileIn, $CharsRead)
    EndIf

    $CrFound = StringInStr($InBlock, @CR)
    $LfFound = StringInStr($InBlock, @LF)

    If $CrFound > 0 And $LfFound > 0 Then
        $Newline = @CRLF
    ElseIf $CrFound > 0 Then
        $Newline = @CR
    Else
        $Newline = @LF
    EndIf
WEnd

; Read first line of text
$InLine = FileReadLine($oFileIn, 1)
$LineCount = 1
FileWriteLine($oFileOut, $InLine & $Newline)

; Read in lines of text until the EOF is reached
While 1
    $InLine = FileReadLine($oFileIn)
    $LineCount += 1
    If @error = -1 Then ExitLoop
    If $LineCount > $NumberOfLines Then ExitLoop
    FileWriteLine($oFileOut, $InLine & $Newline)
WEnd

FileClose($oFileIn)
FileClose($oFileOut)
Opt(“MustDeclareVars”,1);0=否,1=需要预先声明
#包括
#包括
本地$gInPath=$CmdLine[1]
本地$NumberOfLines=$CmdLine[2]
本地$gInDrive、$gInDir、$gInFName、$gInExt、$gOutPath
Local$gMsgBoxTitle=“出现错误”&@ScriptName
本地$InLine
本地$LineCount
莱因当地元
本地$oFileOut
本地$FileStringAppend
本地常量$CHAR\u READ\u块=100
本地$CharsRead=0
本地$CRFOND=0
本地$LfFound=0
本地$Newline
本地$InBlock
如果文件存在($gInPath),则
其他的
MsgBox(4096,$gMsgBoxTitle,“此文件不存在”&@CRLF&$gInPath)
出口
恩迪夫
_路径拆分($gInPath、$gInDrive、$gInDir、$gInFName、$gInExt)
如果$NumberOfLines>=1000000,则
$FileStringAppend=$NumberOfLines/