Sign in to follow this  
Followers 0
tryout1

ADD A SPACE IN A TXT FILE

10 posts in this topic

hi,

How add a space after each character

exemple :

my txt is :

MOTHER

and i want write in txt :

M O T H E R

TX

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$file = FileOpen("test.txt", 0)
$file2 = FileOpen("test2.txt", 2)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file2.")
    Exit
EndIf

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    
;~     MsgBox(0, "Line original:", $line)
    $line = StringSplit($line,'')
    $line2 = ''
    For $i = 1 To $line[0]
        $line2 &= $line[$i] & ' '
    Next
    
;~     MsgBox(0, "Line with spaces:", $line2)
    FileWriteLine($file2, $line2)
Wend

FileClose($file)
FileClose($file2)

EDIT: As start was used example from AutoIt Helpfile for function FileReadLine.

Edited by Zedna

Share this post


Link to post
Share on other sites

$text = "mother"
$Ntext = ""
For $x = 1 To StringLen($text) -1 
    $Ntext &= StringMid($text,$x,1) & " "
Next
$NText &= StringMid($text,$x)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

[autoit]$file = FileOpen("test.txt", 0)

$file2 = FileOpen("test2.txt", 2)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

If $file2 = -1 Then

MsgBox(0, "Error", "Unable to open file2.")

Exit

EndIf

.......

Am I right to assume that Exit closes all open files?

Or do I need to close all files explicitly?

Bart

Share this post


Link to post
Share on other sites

Exit is for quiting the script.

If you want to make the code complete/nice you add at the last closing the files.

Share this post


Link to post
Share on other sites

Am I right to assume that Exit closes all open files?

Or do I need to close all files explicitly?

Bart

Check the helpfile... :D

Remarks

If filehandle is invalid, an AutoIt runtime error will be thrown, and the script will terminate!

Upon termination, AutoIt automatically closes any files it opened--but calling FileClose is still a good idea.

This function is also used to close search handles as returned by FileFindFirstFile().

Share this post


Link to post
Share on other sites

Ah!

Thanks! :wacko:

I checked the Help, but looked at Exit and not at FileClose. :D

Bart

Share this post


Link to post
Share on other sites

Check the helpfile... :D

Ok, so I should explicitly close any open files, but how can I check if a file is open?

When error handling it's not always certain the error occured after the file was opened!

And there is a runtime error when I try to close a file that is not open.

Bart

Share this post


Link to post
Share on other sites

since i like small i made the space adding routine into one line :D

$Ntext = StringTrimRight(StringRegExpReplace($text, '(.*?)*(.)', '\1 '), 1)

Wouter it's nice, but I think not ideal for Newbies :wacko:

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