Jump to content
badeend

FileReadLine not returning values

Recommended Posts

badeend

Hi,

 

I have written this code in order to read a text based file with extension .EDF.

It was possible to do this in the past but now i get no output if i use _Filereadtoarray and if i use filereadline the program does not stop.

The .edf is text based. And in the past i was able to read it (with vba and not autoit).

Is there something different in how autoit reads the text that it can not use it? 

Or am i just an idiot and is my code of?

I attached an example of the file. I have 10000 of these i would like to read and parse.

 

Thanks for you help!

 

#include <File.au3>
#include <Array.au3>
Local $sFilePath =  "\\nippon.be\VDIUserFolders\verheja\Desktop\st\example.edf"
;local $vReturn
;Local $test = _FileReadToArray ( $sFilePath, $vReturn,4,@CRLF)
local $file = FileOpen($sFilePath,16384)
;_ArrayDisplay($vReturn)
;MsgBox(0,0,"")
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
  ConsoleWrite( $line)
Wend

 

example.edf

Share this post


Link to post
Share on other sites
ajag

Just copied from the help file (and modified a little bit):

 

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Local Const $sFilePath = ".\example.edf"

; Open the file for reading and store the handle to a variable.
Local $hFileOpen = FileOpen($sFilePath,  $FO_READ + $FO_FULLFILE_DETECT )
If $hFileOpen = -1 Then
MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf

; Read the contents of the file using the handle returned by FileOpen.
Local $sFileRead = FileRead($hFileOpen)

; Close the handle returned by FileOpen.
FileClose($hFileOpen)

; Display the contents of the file.
MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)

A-Jay


Rule #1: Always do a backup         Rule #2: Always do a backup (backup of rule #1)

Share this post


Link to post
Share on other sites
KickStarter15

@badeend,

First, your in a loop function so basically your code will continue looping until validated .edf does not exist.

Second, doing the ConsoleWrite() function, just add this "@CRLF" to proceed with next line else loop will continue reading the first line.

So here's how:^_^

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

Local $sFilePath =  "\\nippon.be\VDIUserFolders\verheja\Desktop\st\example.edf"
local $file = FileOpen($sFilePath,16384)
$line = FileReadLine($file)

If @error = -1 Then
   MsgBox(0,"Error!", "Error Test!")
   Exit
 Else
   ConsoleWrite($line & @CRLF)
EndIf

 


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
badeend

Thanks for your help!

I tried both codes. The first worked for me. 

Still have a problem with the file though. 

If i use the nonmodified code i get a bunch of numbers (so not the text from the file)

if i modify it so that it writes to console i get the text file with a bunch of NULL in between. 

Why is this different?

If i try to split the string by ":" i get nothing even though it contains a lot of ":".

 

Thanks again,

 

 

Share this post


Link to post
Share on other sites
KickStarter15

@badeend,

Can you post both method you've tried so we can check on it?


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
badeend
#include <array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Local Const $sFilePath = "example.edf"

; Open the file for reading and store the handle to a variable.
Local $hFileOpen = FileOpen($sFilePath,  $FO_READ + $FO_FULLFILE_DETECT )
If $hFileOpen = -1 Then
MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf

; Read the contents of the file using the handle returned by FileOpen.
Local $sFileRead = FileRead($hFileOpen)

; Close the handle returned by FileOpen.
FileClose($hFileOpen)

; Display the contents of the file.
MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)

ConsoleWrite($sFileRead)

If i try this i get 2 different results.

The msgbox and the consolewrite are different...

 

Share this post


Link to post
Share on other sites
KickStarter15
Posted (edited)
55 minutes ago, badeend said:

The msgbox and the consolewrite are different...

Can you extract some of the content of your .edf file? it seems that your .edf has something to do with this reading.

Also, try below code and what do you get.

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

Local $Input

$sFilePath =  @ScriptDir & "\example.edf"
_FileReadToArray($sFilePath, $Input)

For $i = 1 to UBound($Input) -1
   ConsoleWrite($Input[$i] & @CRLF)
Next

 

EDIT:

1 hour ago, badeend said:

If i use the nonmodified code i get a bunch of numbers (so not the text from the file)

Your .edf file is in binary file formatted and based on the checking each code I posted is working. Your .edf file contain one line and was readable by computer and not human letters that is why it returns binary numbers.

Question: What type of line do you want to be read? Or what do you expect to for FileReadLine() letters or numbers.

If letters, then extract/convert your .edf file to text file so we can clearly read what you need. Else, those sample given to you are all working.:)

 

Added something for your reference:

Using StringToBinary() function and BinaryToString() to see and extract the content of your .edf file. As you can see attached screen grab, your .edf file is a ASCII file and also a binary file.:)

 

Extracted1.PNG

Extracted2.png

Extracted3.png

Extracted4.png

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
badeend

Hi,

First of all thanks for your effort!

I need the text like you got with original.

ELIM:3 etc are parts of a .CSV-type of file.

When i run your code I get the binary when i execute the code.

I also tried the binary to string function but this gives me : FCFBLK

So the first word of the file but nothing else.

Share this post


Link to post
Share on other sites
Subz

Just change your encoding e.g.:

#include <array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Local Const $sFilePath = "example.edf"

; Open the file for reading and store the handle to a variable.
Local $hFileOpen = FileOpen($sFilePath, 256)
If $hFileOpen = -1 Then
MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf

; Read the contents of the file using the handle returned by FileOpen.
Local $sFileRead = FileRead($hFileOpen)

; Close the handle returned by FileOpen.
FileClose($hFileOpen)

$sString =  StringRegExpReplace($sFileRead, "[^[:print:]]", "")
$aString = StringSplit($sString, ";")
_ArrayDisplay($aString)

 

Share this post


Link to post
Share on other sites
badeend

SUCCES! 

Thank you!!!!!!

Share this post


Link to post
Share on other sites
KickStarter15
11 hours ago, Subz said:

Just change your encoding e.g.:

#include <array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Local Const $sFilePath = "example.edf"

; Open the file for reading and store the handle to a variable.
Local $hFileOpen = FileOpen($sFilePath, 256)
If $hFileOpen = -1 Then
MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf

; Read the contents of the file using the handle returned by FileOpen.
Local $sFileRead = FileRead($hFileOpen)

; Close the handle returned by FileOpen.
FileClose($hFileOpen)

$sString =  StringRegExpReplace($sFileRead, "[^[:print:]]", "")
$aString = StringSplit($sString, ";")
_ArrayDisplay($aString)

As always, Subz...:lol: nice...


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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

×