Modify

Opened 12 years ago

Closed 12 years ago

#2225 closed Bug (No Bug)

_FileWriteToLine Bug

Reported by: MemphiZ@… Owned by:
Milestone: Component: Standard UDFs
Version: 3.3.8.1 Severity: None
Keywords: Cc:

Description

I found a bug in the _FileWriteToLine function of the File.au3 include file.

If you run the following code and create and new *.txt file it will run just fine. But if you put some text to the last line instead of having an empty last line it will stop working:

#include<File.au3>

$fileLocation = @DesktopDir & "\whatever.txt"
$lineCount = _FileCountLines($fileLocation)

$lineCount += 1
_FileWriteToLine($fileLocation, $lineCount, "Script started at: " & @HOUR & ":" & @MIN & ":" & @SEC, 1)
$lineCount += 1

$var = 0
While 1
	Sleep(30000)
	$var += 0.5
	_FileWriteToLine($fileLocation, $lineCount, $var, 1)
WEnd

I narrowed the error down to this line in the File.au3 include file:

If UBound($aSplit_File) < $iLine Then Return SetError(1, 0, 0)

Hope this helps!

Attachments (0)

Change History (6)

comment:1 Changed 12 years ago by BrewManNH

Can you please explain exactly what you mean by "But if you put some text to the last line instead of having an empty last line it will stop working:", because I ran your script and it worked as expected for me. It added Script started at: 12:46:51 at the end of the text file plus the value of $i and just kept replacing the last lines with that.

comment:2 Changed 12 years ago by MemphiZ <Memphiz@…>

Just open the txt file now and write something in the last line (which should be an empty line.. meaning the cursor is blinking in an empty line) and save it without hitting return..

comment:3 follow-up: Changed 12 years ago by BrewManNH

That's happening because you're telling it to write to a non-existing line number.

In my test text file I put 5 lines of text, the last line entered without hitting Return. _FileCountLines returns 5 lines, trying to write to line 6 ($linecount + 1) causes _FileWriteToLine to try to write to a line that isn't there. _FileCountLines doesn't count the last CR/LF as a line, so if I hit Return on the last line of the file there will be 6 lines in the file, and writing to line 6 doesn't fail. You'll have to make a check to see if the last line of the file is an empty line before trying to write to it because _FileWriteToLine doesn't work when writing to line numbers greater than the actual count of lines in the file.

The bug isn't in the function, but in your interpretation of how _FileCountLines works.

comment:4 in reply to: ↑ 3 ; follow-up: Changed 12 years ago by MemphiZ <Memphiz@…>

I see. Maybe _FileWriteToLine should just write to the last line if the line number specified is lower than the actual LineCount of the file. Anyway, thanks for the infos. This case can be closed.

Replying to BrewManNH:

That's happening because you're telling it to write to a non-existing line number.

In my test text file I put 5 lines of text, the last line entered without hitting Return. _FileCountLines returns 5 lines, trying to write to line 6 ($linecount + 1) causes _FileWriteToLine to try to write to a line that isn't there. _FileCountLines doesn't count the last CR/LF as a line, so if I hit Return on the last line of the file there will be 6 lines in the file, and writing to line 6 doesn't fail. You'll have to make a check to see if the last line of the file is an empty line before trying to write to it because _FileWriteToLine doesn't work when writing to line numbers greater than the actual count of lines in the file.

The bug isn't in the function, but in your interpretation of how _FileCountLines works.

comment:5 in reply to: ↑ 4 Changed 12 years ago by MemphiZ <Memphiz@…>

If the line number specified is HIGHER than the actual LineCount. Sorry.

Replying to MemphiZ <Memphiz@…>:

I see. Maybe _FileWriteToLine should just write to the last line if the line number specified is lower than the actual LineCount of the file. Anyway, thanks for the infos. This case can be closed.

Replying to BrewManNH:

That's happening because you're telling it to write to a non-existing line number.

In my test text file I put 5 lines of text, the last line entered without hitting Return. _FileCountLines returns 5 lines, trying to write to line 6 ($linecount + 1) causes _FileWriteToLine to try to write to a line that isn't there. _FileCountLines doesn't count the last CR/LF as a line, so if I hit Return on the last line of the file there will be 6 lines in the file, and writing to line 6 doesn't fail. You'll have to make a check to see if the last line of the file is an empty line before trying to write to it because _FileWriteToLine doesn't work when writing to line numbers greater than the actual count of lines in the file.

The bug isn't in the function, but in your interpretation of how _FileCountLines works.

comment:6 Changed 12 years ago by trancexx

  • Resolution set to No Bug
  • Status changed from new to closed

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.