Python - encoding/decoding Windows vs Linux - python-2.7

Having this line of code:
print "S\x00A\x00V\x00A"
produces different output. On Windows:
S A V A
and on Linux:
SAVA
What is the difference between the the 2 platform and what can I do to remove the whitespaces from the Windows case?

The difference is at the terminal level.
Windows cmd prints the zero-char as empty whereas your Linux terminal doesn't print it.
Note that in PyScripter console or PyCrust console (wx-based) you only get S (probably because the zero is seen as a line-termination char). So it's definitely not portable :)
To get rid of it just perform a replace:
print("S\x00A\x00V\x00A".replace("\x00",""))

Related

C++ Box drawing characters squeeze together in windows 10 console

I'm trying to print some tables in Windows console using C++.
My codes under default console settings:
cout << "╭─┬╮\n"
"├─┼┤\n"
"╰─┴╯\n";//Code I hope to work
cout << "╭ ─ ┬ ╮\n"
"├ ─ ┼ ┤\n"
"╰ ─ ┴ ╯\n";//Have an extra whitespace behind every character
gave me
The characters only take one space instead of two. They are squeezed together.
I have tried but didn't work:
Change code page to 65001(UTF-8)
Turn on and off every option except legacy mode in cmd settings hoping it would work.
Change font
I then used legacy console mode and this time it work, but for some reason I need it to work in newer mode. Can you tell me how to configure it right or is it just a bug?
OS: Windows 10 1909
Environment: CLion 2021.2.4 + MSVC v142, C++11

How to print in the same position in fortran

Is it possible to replace a character in terminal with another character by changing format of print command or at least clear a line in terminal?
It is not possible in standard Fortran. Perhaps try using the GNU Readline library. Related: Autocomplete from directory in Fortran
If you do not use the Windows terminal you can use the ANSI escape codes. For example, this first prints the stars, than moves back to the same line and writes "test" over the stars:
print *, "***********"
print *,achar(27)//"[2A"
print *,"test"
end
Tested on a Linux terminal, will not work in the basic Windows terminal.

Why does using system("some.exe") in C++ method not work like the command line?

I am writing a program for Windows that eventually has to launch a different pre-existing .exe that sits on the same computer. It passes multiple parameters to this .exe file. I am reading the actual command and parameters and constructing the command but I also tried hard coding it with the same results. Here's the hard coded version (I picked this out of an older C program that uses the same.exe):
system("c://IQapture//dmon2_6_IHD -p2 c://IQapture//mon_table_101_Tx8.txt 11 0 0");
So in the original program inside int _cdecl main(int argc, char**argv) this use of system works. In my C++ program inside a C++ class method when I issue the command the correct program launches but it immediately puts up an error dialog stating that an error has occurred. I echo'd the system string used to launch the exe out to the console. Right after it fails, I copy and paste the same line that was echo'd and this time the exe runs without error. This is repeatable. In case it was timing related I tried adding a 10 second delay before issuing the system command but it didn't matter. Plus the original older program doesn't require a delay. This implies to me that the string is correct and the target program works. Somehow the system() invocation is different from a direct command line invocation. The program compiles and builds fine. I'm using Visual Studio 2010.
Does anyone have ideas on how to make the system() invocation work like the command line invocation?
That really doesn't look like the kind of thing that Windows would be happy with... Try it with backslashes instead:
system("c:\\IQapture\\dmon2_6_IHD -p2 c:\\IQapture\\mon_table_101_Tx8.txt 11 0 0");
If that still doesn't work, you quite likely have one of the following issues:
Your current working directory is wrong;
An environment variable is missing;
Your program is running with the wrong user permissions;
Your program is tying up a resource that the spawned process requires (eg you have not closed a file that it requires as input).
There are a lot of things to consider - the environment, the user running the program, the parent process and what's inherited... Take a look at the parameters to the CreateProcess function. Chances are your system call's invocation isn't matching the command line's (though that may not be the issue, simpler things are more likely.)
I'd advise working backwards from the error to rule out simple causes such as the environment, current directory, etc. before delving into such things as creation flags and security attributes.
You have your slashes backwards. Try:
system("c:/IQapture/dmon2_6_IHD -p2 c:/IQapture/mon_table_101_Tx8.txt 11 0 0");
You can use the backslash \ but because that is an escape sequence starter in a string (for C/C++) that is why you use two in a row. As the compiler will convert \\ into a single slahs in the string:
Thus:
system("c:\\IQapture\\dmon2_6_IHD -p2 c:\\IQapture\\mon_table_101_Tx8.txt 11 0 0");
// Is equivelent to the command line string:
> c:\IQapture\dmon2_6_IHD -p2 c:\IQapture\mon_table_101_Tx8.txt 11 0 0
But Windows has supported both types of slashes for longer than I can remember. So the following command line is equivalent.
> c:/IQapture/dmon2_6_IHD -p2 c:/IQapture/mon_table_101_Tx8.txt 11 0 0
Using '/' in a string (in C/C++) does not require escaping. So you just need to use it as is:
system("c:/IQapture/dmon2_6_IHD -p2 c:/IQapture/mon_table_101_Tx8.txt 11 0 0");

How to get windows xp gui default code page?

The thing is that on non-english versions of XP code pages for console and gui apps differ, for example if I on my Polish version write in console:
echo zażółć gęślą jaźń > somefile.txt
The content of that file is:
zaľ˘† g©lĄ ja«ä
This isn't happening if I put that into cmd file. That is this text with polish letters. But if I create a file or directory that is named using non English only chars, then no matter if it's run from cmd or from finger the effect is always gibberish.
So my question is how to get this second code page that rest of windows is running with. In my case this CP is 1250 - but how to get this number from c++ ?
GetConsoleCP() and GetConsoleOutputCP() both return 852 (which is correct) and GetThreadLocale() gives 1045.
If I try to chcp 1045 it gives "wrong code page".
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage\ACP - this registry key stores the ANSI code page number (it should be 1250 in your case) and HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage\OEMCP stores OEM code page.
Oh, and there are GetACP and GetOEMCP functions.

How to properly use system() to execute a command in C++?

I am new to C++ programming under Windows. I am trying to execute a command say cuobjdump in C++ code using the system() function:
system("C:\\program files\\nvidia gpu computing...\\cuobjdump.exe --dump-cubin C:\\..\\input.exe");
output:
Usage : cuobjdump [options] <file>
This followed by the list of the options for cuobjdump.
When I execute this program I always get the cuobjdump help options displayed in the command line. It's as if the system call does not parse the filename. What am I doing wrong? I get the same result while using createprocess. The options --dump-cubin gives an error as if I mistyped it.
Give a try with (that is, surrounding cuobjdump.exe path with ", properly escaped in C++ as \"):
system("\"C:\\program files\\nvidia gpu computing...\\cuobjdump.exe\" --dump-cubin C:\\..\\input.exe");
system("cuobjdump --dump-cubin path\filename.exe");
That \f is interpreted by the compiler as a string escape sequence, try path\\filename.exe
Most obviously, \ is an escape character in C / C++ strings, so it has to be doubled if you want to use it literally.
system("cuobjdump --dump-cubin path\\filename.exe");
Assuming that path is correct, you have to use a double \\ within strings to represent a single \.
I suggest you to use CreateProcess, or ShellExecute / ShellExecuteEx since you are working on Windows. system and ShellExecute eventually calls CreateProcess only.