sree161

Count in a line shows more characters than available

12 posts in this topic

#1 ·  Posted

Hi

I am trying to read a line from a file. Post read i am counting the number of characters in the line by using StringLen.

The line is sc marks:1
Which is 10 letters but the string length is returning length as 14.
Can i know the reason behind it??

I wanted to pick the last number in the line.
Can i get any solutions to the problem i am facing???
                  

Share this post


Link to post
Share on other sites



#2 ·  Posted

Can I suggest you write a short sample code, illustrating this. I suspect that in the process of doing that you will find your problem:

Step one: set a $string = " sc marks:1", StringLen and msgbox the result. Then cut and paste the same string into a file and repeat.

Perhaps you have a special character or text encoding in your file? Even if you don't find your problem it makes it quick for us to see exactly what you have tried and its output.


Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites

#3 ·  Posted

1 hour ago, SlackerAl said:

Can I suggest you write a short sample code, illustrating this. I suspect that in the process of doing that you will find your problem:

Step one: set a $string = " sc marks:1", StringLen and msgbox the result. Then cut and paste the same string into a file and repeat.

Perhaps you have a special character or text encoding in your file? Even if you don't find your problem it makes it quick for us to see exactly what you have tried and its output.

Hi

i have tried this code
#include <MsgBoxConstants.au3>
#include <File.au3>
$File = @Dir \Text.txt
$Open = FileOpen($File, 0)
$Read = FileRead($Open)
MsgBox ($MB_SYSTEMMODAL, "read:", $Read )
$len = StringLen($Read)
MsgBox ($MB_SYSTEMMODAL, "Length:", $len )

i have copied text to source file and then closed it by saving it.

 

Text file:

1 makers:1

Value returned is 14
total number of characters present are 10

Share this post


Link to post
Share on other sites

#4 ·  Posted

sree161,

Please post a copy of the file itself - my guess is that there are some additional EOLs within it.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi.

$File = @Dir \Text.txt

couldn't be correct. What exactly is the path to the file?

Do you mean:

$File = @ScriptDir & "\Text.txt"

Conrad

Edited by Simpel
code in tags

SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites

#6 ·  Posted

Hi,

$Read = FileRead($Open)

reads the whole text, not a single line. Is there another line?

You wrote 10 letters. Are there Whitespaces? StringLen counts them too.

$sString = "123 45 6789"
MsgBox(0,StringLen($sString), $sString)

 

Share this post


Link to post
Share on other sites

No problems on my system.
Suggestion to run below to see AutoIt works fine (be aware it creates  test.txt automatically overwriting any existing file)

#include <MsgBoxConstants.au3>
#include <File.au3>
#include <String.au3>

$File = @ScriptDir & "\Test.txt"

; Open the file for writing (append to the end of a file) and store the handle to a variable.
Local $hFileOpen = FileOpen($File,$FO_OVERWRITE + $FO_UTF8)
If $hFileOpen = -1 Then
   MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
   Return False
EndIf

filewrite($hFileOpen,"sc marks:1" )
fileclose($hFileOpen)

$Open = FileOpen($File, 0)
$Read = FileRead($Open)
consolewrite("read:" & $Read & @CRLF)
$len = StringLen($Read)
consolewrite("Length:" & $len & @CRLF )
$blen = binaryLen($Read)
consolewrite("Binary length:" & $blen & @CRLF )
$hexString=_stringtohex($read)
consolewrite("hex:" & $hexString & @CRLF )

 

Share this post


Link to post
Share on other sites

Hi all,
I used a hotkey to paste data to my notepad due to which the pointer goes to next line after pasting.
Will this be the reason for the problem i am facing???

Share this post


Link to post
Share on other sites

#10 ·  Posted

On 8/31/2017 at 8:42 PM, Simpel said:

Hi.

$File = @Dir \Text.txt

couldn't be correct. What exactly is the path to the file?

Do you mean:

$File = @ScriptDir & "\Text.txt"

Conrad

Sorry that was a typo..

Share this post


Link to post
Share on other sites

#11 ·  Posted

4 hours ago, sree161 said:

Hi all,
I used a hotkey to paste data to my notepad due to which the pointer goes to next line after pasting.
Will this be the reason for the problem i am facing???

please try what i suggested in post #8.

Show your code for the hotkey (i assume for the moment its just ctrl+v)

Will this be the reason for the problem i am facing???

Most likely you indeed have special characters in your string like a Start of file (BOM),  Some messing up with unicode/ascii/.... characterset. But from what you describe its impossible to reveal your problem (you have to attach the file you read or test post#8). Each line can have a CRLF CRLF which is then 4 characters invisible to enduser.

Share this post


Link to post
Share on other sites

#12 ·  Posted

Anyway whatever the number of eol white spaces a regular expression does the job  :)

$string = "  sc marks:1  " & @crlf & @crlf

$n = StringRegExp($string, '(\d+)\s*$', 1)
If not @error Then Msgbox(0,"", $n[0])

 

1 person likes this

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