Jump to content

Reading a file to an array and outputting it in a text file...


Recommended Posts

Hi All,

I am trying to read a simple text file (small.txt) (attached) to an array and put all it's content in another text file (output.txt). File are not identical (I have compared them). Looks like that in the output.txt file, there is always an extra line?

Why?

Can it be do without this extra line???

Thanks,

#include <file.au3>

Dim $aRecords

If Not _FileReadToArray("c:\small.txt",$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array! error:" & @error)

Exit

EndIf

$x=Fileopen ("c:\output.txt",2)

For $i=1 to 3

If $i<>3 Then

Filewriteline ($x,$arecords[$i] & @LF)

Else

Filewriteline ($x,$arecords[$i] & @CR)

EndIf

Next

small.txt

Link to comment
Share on other sites

follow the process that help gives you

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next

thus

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

$i=Fileopen ("c:\output.txt",2)

For $x = 1 to $aRecords[0] -1
    Msgbox(0,'Record:' & $x, $aRecords[$x])
    Filewriteline ($i,$arecords[$x] & @CR) 
Next

Filewriteline ($i,$arecords[$x] )

oh yea and file close

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

$i=Fileopen ("c:\output.txt",2)

For $x = 1 to $aRecords[0] -1
    Msgbox(0,'Record:' & $x, $aRecords[$x])
    Filewriteline ($i,$arecords[$x] & @CRLF)
Next

Filewriteline ($i,$arecords[$x] )

this does it

8)

NEWHeader1.png

Link to comment
Share on other sites

The file "small.txt" is in c:\

This is (your) script I'm running( changed the line you mentioned)

#include <file.au3>

Dim $aRecords

If Not _FileReadToArray("c:\small.txt",$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

$i=Fileopen ("c:\output.txt",2)

For $x = 1 to $aRecords[0] -1

Msgbox(0,'Record:' & $x, $aRecords[$x])

Filewriteline ($i,$arecords[$x] & @CRLF)

Next

Filewriteline ($i,$arecords[$x] )

Attached is a screenshot confirming that they are not identical and that there is an extra empty line at the end of output.txt. Looks like it was created when file was opened?

Link to comment
Share on other sites

Thanks, man. It does the trick...

The issue is that I have a BIG text file to read and must use FileWriteLine to write in the output file, since I will not want to write the entire file (only sections of it depending of a condition) I will like to use the FileReadToArray... Possible?

Link to comment
Share on other sites

this will do it..

$File_1 = "c:\small.txt"
$File_2 = "c:\output.txt"

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray($File_1,$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

$i=Fileopen ($File_2,2)

For $x = 1 to $aRecords[0] -1
    Filewriteline ($i,$arecords[$x] & @CRLF)
Next

Filewrite ($i, ($arecords[$x]))

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks Valuater... This is close enough :P

On the posted script, if you open Notepad, files seems indentical, however, if you compare them using total commander, you will observe that there is an empty line (god knows from where it came) between each written line... I have overcome it by changing from Filewriteline ($i,$arecords[$x] & @CRLF) to Filewriteline ($i,$arecords[$x] & @LF)

Thanks, dude :lmao:

Link to comment
Share on other sites

Although it works, it strange since in the HELP file it is written: "If the line does NOT end in @CR or @LF then a DOS linefeed (@CRLF) will be automatically added.", so why in the last line of code posted there is no empty line as it should be (since there is no @CR ot @LF in the code line)

Hmmmm....

Link to comment
Share on other sites

Although it works, it strange since in the HELP file it is written: "If the line does NOT end in @CR or @LF then a DOS linefeed (@CRLF) will be automatically added.", so why in the last line of code posted there is no empty line as it should be (since there is no @CR ot @LF in the code line)

Hmmmm....

you quoted

FileWriteLine

see

FileWrite

( no @CR included)

8)

NEWHeader1.png

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