Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SHELL creates unicode file, can't read correctly with LINE INPUT
#1
Hi, 

I have this snippet to get the number of processor cores.
Problem is that the SHELL command produces a unicode text file and LINE INPUT is not able to read the text correctly.
Manually converting tmp.txt to an ascii file solves the problem.
Question is why is the file created as unicode by default?
Can I influence this from within QB64?
OS is Win 11.
Btw.: I tried also OPEN ... FOR BINARY - doesn't change anything (e.g. LINE INPUT still can't read UNICODE as text):

Code: (Select All)
    returncode% = SHELL("wmic cpu get NumberOfCores >tmp.txt")
   
    IF _FILEEXISTS("tmp.txt") THEN
        OPEN "tmp.txt" FOR INPUT AS #1
            DO UNTIL EOF(1)
                LINE INPUT #1, a$
                ?">";a$;"<";filecount%;",len=";len(a$)
                filecount% = filecount% + 1
            LOOP
        CLOSE #1
    END IF

    'KILL "tmp.txt"
Reply
#2
Welcome to the forums.

You might have to create a second temporary file, after the output of the first "SHELL". I'm not sure where "strings.exe" program is located but you could write a "SHELL" command like this:

Code: (Select All)
SHELL "strings.exe tmp.txt > tmp2.txt"
IF _FILEEXISTS("tmp2.txt") THEN
:
END IF
KILL "tmp2.txt"
KILL "tmp.txt"

This "strings.exe" program comes packaged with QB64, somewhere with "g++" compiler, preprocessor, library maker and other utilities. I use Linux regularly and therefore have forgotten a lot of things about Windows, and I have refused to upgrade from Windows10 21H2 on a different computer.

The path should be: "(qb64path)/internal/c/c_compiler/bin" or something like that. You could do a search in File Explorer for EXE files into "(qb64path)/internal/c". The "(qb64path)" is a placeholder for the place on your system what is called the "home" directory of QB64. This should be somewhere in your user account, not "Program Files" and even less "Program Files (x86)" if it exists, nor anywhere else on C: drive. It should be something that looks like "C:/Users/(yourname)/qb64" to begin with.

EDIT: set your PATH temporarily to "(qb64path)/internal/c/c_compiler/bin" before running your program which should be something like this:

Code: (Select All)
returncode% = SHELL("wmic cpu get NumberOfCores >tmp2.txt")
    returncode% = SHELL("strings.exe tmp2.txt >tmp.txt")
  
    IF _FILEEXISTS("tmp.txt") THEN
        OPEN "tmp.txt" FOR INPUT AS #1
            DO UNTIL EOF(1)
                LINE INPUT #1, a$
                ?">";a$;"<";filecount%;",len=";len(a$)
                filecount% = filecount% + 1
            LOOP
        CLOSE #1
    END IF

    'KILL "tmp.txt"
    'KILL "tmp2.txt"
Reply
#3
Try piping the output through MORE, which will output plain old text:


[Image: 2023-05-05-202953.png]


Code: (Select All)
'JSR sez: changed returncode to _UNSIGNED _INTEGER64, added line to print it & dump it to a file.
'When compiled with QB64PE 32-bit, returncode~&& is different every time I run this program (typically 106AC990000ACBC, but the first 4 hex digits are different each time).
'When compiled with QB64PE 64-bit, returncode~&& is ACBC.  Still a mystery.
'Haven't had much luck decoding that return code on Google just yet.

returncode~&& = Shell("wmic cpu get NumberOfCores | more >tmp.txt")

Print Hex$(returncode~&&)
o$ = "echo " + Hex$(returncode~&&) + ">TestForTheSolarCode_returncode.txt"
Shell (o$)


If _FileExists("tmp.txt") Then
    Open "tmp.txt" For Input As #1
    Do Until EOF(1)
        Line Input #1, a$
        Print ">"; a$; "<"; filecount%; ",len="; Len(a$)
        filecount% = filecount% + 1
    Loop
    Close #1
Else
    Print "tmp.txt not found"
End If

'KILL "tmp.txt"

FWIW: I had to run the WMIC command on the command line to test it.  Trying it with the QB64PE (32-bit, hmm) Shell function on my Win7 Pro 64-bit system returns a code and fails to create tmp.txt.
(Update: OK, I just tested this program on QB64PE-64, and it is failing with a code of ACBC now.  No idea what _that_ means.) (This is my final edit... I pinkie-swear.)

FWIW2: https://research.nccgroup.com/2022/03/10...owershell/
Reply
#4
(05-06-2023, 01:48 AM)JRace Wrote: Try piping the output through MORE, which will output plain old text:


[Image: 2023-05-05-202953.png]


Code: (Select All)
'JSR sez: changed returncode to _UNSIGNED _INTEGER64, added line to print it & dump it to a file.
'When compiled with QB64PE 32-bit, returncode~&& is different every time I run this program (typically 106AC990000ACBC, but the first 4 hex digits are different each time).
'When compiled with QB64PE 64-bit, returncode~&& is ACBC.  Still a mystery.
'Haven't had much luck decoding that return code on Google just yet.

returncode~&& = Shell("wmic cpu get NumberOfCores | more >tmp.txt")

Print Hex$(returncode~&&)
o$ = "echo " + Hex$(returncode~&&) + ">TestForTheSolarCode_returncode.txt"
Shell (o$)


If _FileExists("tmp.txt") Then
    Open "tmp.txt" For Input As #1
    Do Until EOF(1)
        Line Input #1, a$
        Print ">"; a$; "<"; filecount%; ",len="; Len(a$)
        filecount% = filecount% + 1
    Loop
    Close #1
Else
    Print "tmp.txt not found"
End If

'KILL "tmp.txt"

FWIW: I had to run the WMIC command on the command line to test it.  Trying it with the QB64PE (32-bit, hmm) Shell function on my Win7 Pro 64-bit system returns a code and fails to create tmp.txt.
(Update: OK, I just tested this program on QB64PE-64, and it is failing with a code of ACBC now.  No idea what _that_ means.) (This is my final edit... I pinkie-swear.)

FWIW2: https://research.nccgroup.com/2022/03/10...owershell/

Thank you, quite elegant solution.
Reply




Users browsing this thread: 3 Guest(s)