Jump to content

The Last Character in a file


Recommended Posts

Is it possible to read the last character in a file and if it isn't an expected value, add it to the end?

Thanks in advance

This will give you the last character:

#include <File.au3>

Global $Array
Global $lines_count
Global $Text_line
Global $Chr_count
Global $Chr_last


_FileReadToArray( "Text.txt" , $Array );;Reading the file to an Array
If (not IsArray( $Array )) or (@error) Then
    Exit
EndIf

$lines_count  =  $Array [0];;Number of lines in the file.

;Now will take the last line.
$Text_line = $Array[$lines_count]

;Removing white spaces becuase you don't want the last character to be a white space.
$Text_line = StringStripWS( $Text_line , 8 )

;Now will split all the characters in the line and save to $Array
$Array = StringSplit( $Text_line , '' )

$Chr_count = $Array[0];;Number of characters in the last line.

$Chr_last = $Array[$Chr_count];This is the last character.

MsgBox( 0 , "Last Character is:" , $Chr_last )
Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Thank you very much!!! Example: the last caharcter is '.' but i need this file finish with '<009>', is it possible to use FileWriteLine?

In the case that i'm working with strings, is it possible to retrieve the last character? And append some characters to the end?

Link to comment
Share on other sites

Thank you very much!!! Example: the last caharcter is '.' but i need this file finish with '<009>', is it possible to use FileWriteLine?

In the case that i'm working with strings, is it possible to retrieve the last character? And append some characters to the end?

Read the help about FileWriteLine() and FileWrite(), FileWriteLine() will append another line to your file and then add '<009>'.

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

  • Moderators

This will give you the last character:

#include <File.au3>

Global $Array
Global $lines_count
Global $Text_line
Global $Chr_count
Global $Chr_last
_FileReadToArray( "Text.txt" , $Array );;Reading the file to an Array
If (not IsArray( $Array )) or (@error) Then
    Exit
EndIf

$lines_count  =  $Array [0];;Number of lines in the file.

;Now will take the last line.
$Text_line = $Array[$lines_count]

;Removing white spaces becuase you don't want the last character to be a white space.
$Text_line = StringStripWS( $Text_line , 8 )

;Now will split all the characters in the line and save to $Array
$Array = StringSplit( $Text_line , '' )

$Chr_count = $Array[0];;Number of characters in the last line.

$Chr_last = $Array[$Chr_count];This is the last character.

MsgBox( 0 , "Last Character is:" , $Chr_last )

oÝ÷ Ûú®¢×mmç²'«¶î²×h¶¬jëh×6$LastCharacter = StringRight(FileRead('SomeTxtFile.txt'), 1)
MsgBox(64, 'Info', $LastCharacter)oÝ÷ ØGb´-¹©eÊØb±Ú³Z·*.­«$xºÚ"µÍÑ[TXÙSÝÚ ÌÎNÑ[SØØ][Û   ÌÎNË ÌÎNÉÌIÝÉÌÎNÊB[ÈÑ[TXÙSÝÚ   ÌÍÚ[S[YK ÌÍÜÐÚXÚÊBSØØ[  ÌÍÜÑXYH[TXY
    ÌÍÚ[JBRYÝ[ÔYÚ
    ÌÍÜÑXYÝ[Ó[    ÌÍÜÐÚXÚÊJH   ÉÝÈ  ÌÍÜÐÚXÚÈ[BIÌÍÚÜ[H[SÜ[   ÌÍÚ[KBBQ[UÜ]J   ÌÍÚ[S[YKÝ[Õ[TYÚ
    ÌÍÜÑXYJH    [È ÌÍÜÐÚXÚÊBBQ[PÛÜÙJ ÌÍÚÜ[BQ[Y[Y
(This FileRead() option without FileGetSize() needs Beta)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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