sree161 Posted August 31, 2017 Share Posted August 31, 2017 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??? Link to comment Share on other sites More sharing options...
SlackerAl Posted August 31, 2017 Share Posted August 31, 2017 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. Link to comment Share on other sites More sharing options...
sree161 Posted August 31, 2017 Author Share Posted August 31, 2017 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 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 31, 2017 Moderators Share Posted August 31, 2017 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Simpel Posted August 31, 2017 Share Posted August 31, 2017 (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 August 31, 2017 by Simpel code in tags SciTE4AutoIt = 3.7.3.0 AutoIt = 3.3.14.2 AutoItX64 = 0 OS = Win_10 Build = 19044 OSArch = X64 Language = 0407/german H:\...\AutoIt3\SciTE H:\...\AutoIt3 H:\...\AutoIt3\Include (H:\ = Network Drive) Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. Link to comment Share on other sites More sharing options...
nobbitry Posted August 31, 2017 Share Posted August 31, 2017 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) Link to comment Share on other sites More sharing options...
junkew Posted August 31, 2017 Share Posted August 31, 2017 Use this then you can easily see what is counted https://www.autoitscript.com/autoit3/docs/libfunctions/_StringToHex.htm FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
junkew Posted September 1, 2017 Share Posted September 1, 2017 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 ) FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
sree161 Posted September 4, 2017 Author Share Posted September 4, 2017 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??? Link to comment Share on other sites More sharing options...
sree161 Posted September 4, 2017 Author Share Posted September 4, 2017 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.. Link to comment Share on other sites More sharing options...
junkew Posted September 4, 2017 Share Posted September 4, 2017 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. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
mikell Posted September 4, 2017 Share Posted September 4, 2017 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]) sree161 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now