Sign in to follow this  
Followers 0
ramseier

_FileCountLines delivers wrong value

10 posts in this topic

Hello,

I have a textfile with csv-data which has 604 lines (Ultraedit).

If i use _FileCountLines($filename) it delivers the following value: "-351489"

If i read the file into an array, the file has only 261 lines.

I've tried it as it is and converted unix2dos.

Every other file delivers correct filecounts.

Whats wrong with this file?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Can we see the file and code you are using?

Try this:

#include <File.au3>

$File = FileOpenDialog("_FileCountLines", @ScriptDir, "All Files(*.*)")

MsgBox(0, "_FileCountLines", _FileCountLines($File))

Probably makes no difference.

Edited by JamesB

Share this post


Link to post
Share on other sites

I had the same problem. One legacy program is outputting test results into csv file, and I'm monitoring this file and using _FileCountLines and FileReadLine to get last line. For small files this was working good, but when file gets larger, _FileCountLines start to return minus values.

I'm using this piece of code to do this task now:

CODE

$f = FileRead("RESULTS.CSV")

$p1 = StringInStr($f, Chr(13) & Chr(10), 1, -2)

$p2 = StringInStr($f, Chr(13) & Chr(10), 1, -1)

MsgBox(0, "String", StringMid($f, $p1, $p2 - $p1))

Maybe it will be of some help to you.

Share this post


Link to post
Share on other sites

I have the same problem, if I use the function against an excel file it outputs negative numbers, but if I output the file to a txt file it works fine.

Share this post


Link to post
Share on other sites

Perhaps there are some hidden characters in the file that you aren't seeing. Maybe you can open the file in SciTe and click View > End Of Line, or open it in Word and enable code view.

Share this post


Link to post
Share on other sites

Sorry for bumping such an old thread but I used this function for the first time today and also found that it reported the wrong number of lines for all the files I tested it with.

As an example I got 2 plain text files, 1st file has 888 lines (according to Notepad++) but _FileCountLines() returned 882. 2nd file has 959 lines but _FileCountLines() returned 953. I was thinking that the function doesn't count empty lines but even after removing the empty lines the reported number of lines was wrong so I wrote my own replacement which gives me the correct result, at least, for the files I am currently working with (plain ANSI text, DOSWindows).

Func _FCntLn($sFile)
    Local $ret = 0, $hFile = FileOpen($sFile, 0)
    While 1
        FileReadLine($hFile)
        If @error = -1 Then ExitLoop
        $ret += 1
    WEnd
    FileClose($hFile)
    Return $ret
EndFunc

Share this post


Link to post
Share on other sites

 

Sorry for bumping such an old thread but I used this function for the first time today and also found that it reported the wrong number of lines for all the files I tested it with.

As an example I got 2 plain text files, 1st file has 888 lines (according to Notepad++) but _FileCountLines() returned 882. 2nd file has 959 lines but _FileCountLines() returned 953. I was thinking that the function doesn't count empty lines but even after removing the empty lines the reported number of lines was wrong so I wrote my own replacement which gives me the correct result, at least, for the files I am currently working with (plain ANSI text, DOSWindows).

Func _FCntLn($sFile)
    Local $ret = 0, $hFile = FileOpen($sFile, 0)
    While 1
        FileReadLine($hFile)
        If @error = -1 Then ExitLoop
        $ret += 1
    WEnd
    FileClose($hFile)
    Return $ret
EndFunc

That's a very slow way to do it. 

The _FileCountLines function does not include any trailing space. That could be the source of the discrepancy. 

1 person likes this

Share this post


Link to post
Share on other sites

That's a very slow way to do it. 

The _FileCountLines function does not include any trailing space. That could be the source of the discrepancy. 

I've used that function a lot, and can confirm that the only discrepancies that I've ever come across, have indeed been trailing space after the last text line.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

That's a very slow way to do it. 

The _FileCountLines function does not include any trailing space. That could be the source of the discrepancy. 

 

No it's not, it's even faster (at least for small files).

 _FCntLn               = 888 lines (4.82992862113775 ms)

 _FileCountLines   = 882 lines (4.980045943599 ms)

 _FCntLn               = 959 lines (3.82799172276179 ms)

 _FileCountLines   = 953 lines (6.60824151798637 ms)

 _FCntLn               = 959 lines (3.83261071729906 ms)

 _FileCountLines   = 953 lines (6.03933535747938 ms)

 _FCntLn               = 888 lines (3.27640679176957 ms)

 _FileCountLines   = 882 lines (4.91884426598018 ms)

Times taken using TimerInit() and TimerDiff(). If trailing whitespace is causing this function to report the wrong number of lines then this might need to be fixed or at least mentioned in the help file.

Edited by Tacomas

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  
Followers 0