Jump to content
Sign in to follow this  
BlueScreen

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

Recommended Posts

BlueScreen

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

Share this post


Link to post
Share on other sites
Valuater

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

Share this post


Link to post
Share on other sites
BlueScreen

Thanks, Valuater...

I have run your posted script, files small.txt and output.txt are not identical.

Try it...

Share this post


Link to post
Share on other sites
Valuater

#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

Share this post


Link to post
Share on other sites
BlueScreen

Sorry, Valuater...

I have compared the files using Total commander. Files are NOT identical... :P

Share this post


Link to post
Share on other sites
Valuater

ok...

this does the trick

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

$file_info = FileRead($File_1, FileGetSize($File_1))
FileWrite($File_2, $file_info)

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites
BlueScreen

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?

Share this post


Link to post
Share on other sites
BlueScreen

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?

Share this post


Link to post
Share on other sites
Valuater

yes... lete me take another look at what i can do

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
BlueScreen

Thanks!!!!! I will also post my entire script soon...

Share this post


Link to post
Share on other sites
Valuater

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

Share this post


Link to post
Share on other sites
BlueScreen

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:

Share this post


Link to post
Share on other sites
BlueScreen

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

Share this post


Link to post
Share on other sites
Valuater

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

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.