Sign in to follow this  
Followers 0
Ventura

FileWriteToLine doesn't work as expected

5 posts in this topic

#include <File.au3>
Global $line = 2, $num = 1

Do
_FileWriteToLine(@ScriptDir & "\data.txt", $line, 'Name'&@CRLF&'<ID>"'&$num&'"', 0)
$line = $line + 3
$num = $num + 1
Sleep(100)
Until $line = 14

Like the title says, I need _FileWriteToLine to write data into a text file that already has this text 

data----------------
data----------------

What I need is to put informations in between the two "data------------------"

data----------------
Name
<ID>"1"
Name
<ID>"2"
Name
<ID>"3"
Name
<ID>"4"
data----------------

So it looks like the above example,  but it doesn't, instead it just puts 1 information between the "data------------------", while I want the 4 informations in it.

There is nothing wrong with the loop, I have tested it with msgbox and nothing wrong with it absolutely.. so I think it must be about _FileWriteToLine function but not so sure.

Share this post


Link to post
Share on other sites



This seems so extremely inefficient because every time you call _FileWriteToLine the function is going to open the file at the start and close the file when it's done. The reason why it doesn't work is because the line you specified goes out of bounds of the lines available in the file. On the first pass through it writes to line 2 (valid line) adding 2 more lines, now the file is a total of 4 lines  (data-----, name, <id>"n", data----) then you add 3, making $line = 5. Now you're out of bounds and the calls to _FileWriteToLine all fail (using some debug code you would see that the function returns 0 and sets the error flag to 1. From the help file

Quote

Return Value

Success: 1.
Failure: 0 and sets the @error flag to non-zero.
@error: 1 - File has fewer lines than $iLine
2 - File does not exist
3 - Error when opening file
4 - $iLine is invalid
5 - $iOverWrite is invalid
6 - $sText is invalid

So, there is something wrong with your loop, there's absolutely nothing wrong with the function ;)

This would be a bit of a more efficient way to write what you're wanting to do.

#include <File.au3>

Global $sText = ""

For $num = 1 to 4
    $sText &= 'Name' & @CRLF & '<ID>"' & $num & '"' & @CRLF
Next

_FileWriteToLine(@ScriptDir & "\data.txt", 2, StringTrimRight($sText, 2), 0)

Share this post


Link to post
Share on other sites

The problem with your script is that you're jumping 3 lines, but only inserting 2, so after the first loop through you're trying to write to lines of the file that don't exist.

; Change 
$line = $line + 3 
; To
$line = $line + 2
; Or even better
$line += 2

 

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

16 minutes ago, InunoTaishou said:

So, there is something wrong with your loop, there's absolutely nothing wrong with the function ;)

This would be a bit of a more efficient way to write what you're wanting to do.

Well then it was because of the function ... but the loop has done a lot of things for me with no problem.

What Brew just said and only slight change made it work with my loop.

Edited by Ventura

Share this post


Link to post
Share on other sites

Both works thanks..

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