Jump to content

_FileCountLines delivers wrong value


ramseier
 Share

Recommended Posts

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?

Link to comment
Share on other sites

  • 3 weeks later...

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.

Link to comment
Share on other sites

  • 5 years later...

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
Link to comment
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. 

Link to comment
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.

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

Link to comment
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. 

 

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