`sscanf` not reading doubles properly when compiled locally [duplicate] - c++

This question already has answers here:
How to get system's decimal separator character using STL?
(1 answer)
How can I set the decimal separator to be a comma?
(2 answers)
Closed 4 months ago.
I've been given some C++ code which (among other things) reads filenames and parses them using sscanf. However, despite using %lf for parsing, sscanf parses the first double truncated, and then fails to parse the rest of the string.
The offending code segment is:
int row, col;
double camx, camy;
char ext[64];
char inp[64] = "out_00_00_-381.909271_1103.376221.png";
int ret = sscanf(inp, //files[i].toStdString().c_str(),
"out_%d_%d_%lf_%lf%s", &row, &col, &camy, &camx, ext);
I get the following parse:
ret=3
row=0, col=0
camy=-381
camx=[gibberish]
ext=[gibberish]
More confusingly, if I run the same code segment in repl.it, it works perfectly.
I'm compiling using CMake on Ubuntu 20.04, GCC 9.4.0 x86_64-linux-gnu. I don't fully understand CMake (since someone else gave me this repo), but I have the following CMakeLists.txt:
include_directories("${PROJECT_SOURCE_DIR}/include" ${OpenCL_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS})
QT5_WRAP_CPP(lf_renderer_HEADERS_MOC LFViewWidget.h LFViewWindow.h)
add_executable(task5_lf_renderer main.cpp LFViewWindow.cpp LFViewWidget.cpp LightFieldRenderer.cpp ${lf_renderer_HEADERS_MOC})
target_compile_features(task5_lf_renderer PRIVATE cxx_range_for)
target_link_libraries(task5_lf_renderer ${OpenCL_LIBRARIES} Qt5::Widgets) #Qt5::OpenGL
where this file is LightFieldRenderer.cpp in the add_executable() call.
I suspect the issue is that some compile flags are being set/unset somewhere which is causing this to compile differently on my machine. Any pointers to where I should look would be much appreciated!

Related

Why does my c++ program output garbled code

I use MinGW64 to compile c++ programs. But since I upgraded to Windows 10, I found my c program output Chinese will be garbled code.
I follow the online method, adding a code in the program header: SetConsoleOutputCP(65001);, then it fixes. but I think it's so troublesome to do this for each c++ program. What should I do?
I think this is my system's problem, the same code in Windows 7 is not a problem, I just want to find a more convenient solution instead of adding the same code to every file
There's the code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
SetConsoleOutputCP(65001);
cout << "文本" ; //Output will be garbled code if there's no line above
return 0;
}
The console in most operating systems only expects ASCII character input. In order to show some other char set you have to specify that in your code. The SetConsoleOutputCP command sets the "code page" windows should read from. By the way not all versions of windows have the same code for this command.
Please refer to the documentation found here.
The documentation suggests using EnumSystemCodePages to make sure the code for that language exists on that system.
P.S.
Your English is very good :)
EDIT
I tested your code on my computer with Visual Studio 2019 and got the following
Warning C4566 character represented by universal-character-name '\u6587' cannot be represented in the current code page (1255)
even with the SetConsoleOutputCP command you added. I assume you need to have chines installed for this to work. The problem is that I don't have the relevant code page for the windows console to look in for the char set. see this answer and this answer.

Gnu debugger can't remove variable from display? [duplicate]

This question already has answers here:
GDB: How to remove a variable from the auto display
(2 answers)
Closed 5 years ago.
I am working on a c++ program on linux ubuntu 16.04 and I've done a tutorial on gnu debugger.
I am having some problems with my code and as I step through it it's easier for me to compare two variables at each part fullPath and argv[1]
But once I get past that particular segment I want to remove argv[1].
I called them with the following:
display argv[1]
display fullPath
But when I try to remove argv[1] with undisplay argv[1] I get an error that reads the following:
warning: bad display number at or near 'argv[1]'
It still continues to display argv[1] unless I exit debugger and start it again without displaying it. Is there a way to fix this?
NOTE
I've also tried delete argv[1] which also doesn't work.
The undisplay command is expecting a list number, not an expression. You can see the list numbers for all your auto-display expressions by typing:
info display
Let's say that argv[1] is assigned item 3 in that list. You would then remove it with:
undisplay 3

How can I insert an input in sublime compiler [duplicate]

This question already has answers here:
Sublime Text with console input for c++ programs
(2 answers)
Closed 8 years ago.
I started to use sublime text 2 editor today to write c/c++ programs, but when I run this code
#include <cstdio>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}
I am expecting the compiler to ask for an input.
but instead, it just prints the initial value of (n) which is garbage.
How can i insert an input ?
I suppose what you want to do is input data in the same panel where Sublime Text show results, well friend, you can't.
The only thing you can do is configure Sublime Text for running the compiled program in an external terminal. The link chris provide you is a good start.
On the other hand when you say: expecting the compiler to ask for an input, is a conceptual error. The compiler is not whom is expecting the input. The input is being expected by the compiled program.

Getting weird error using g++ compiler [duplicate]

This question already has answers here:
Compilation error: stray ‘\302’ in program, etc
(12 answers)
Closed 8 years ago.
When I try to compile my code using g++ in terminal, I get an error:
Decoder.cpp:32:1: fout: stray ‘\302’ in program
Decoder.cpp:32:1: fout: stray ‘\262’ in program
Decoder is a class that decodes lines from an inputfile. I have no idea what these errors can be. When I look on the internet, they say it has something to do with whitespaces. No clue why! I used to write empty spaces to an output file: like this out << " ";. But I never got an error untill now.
So I went to eclipse, and my code runs fine. Back to g++ and I get the same error. Any idea why?
You've somehow ended up with a non-ASCII character (Unicode "superscript two") in line 32 of your source. Delete that character, replace it with whatever is supposed to be there (a normal 2, perhaps? or maybe nothing?), and be careful where you copy and paste code from.
There could be invisible control character on line 32. Please rewrite the complete line. That should fix your compilation issue.
You may as well refer: https://stackoverflow.com/a/6572148/815812

swprintf fails with unicode characters in xcode, but works in visual studio

While trying to convert some existing code to support unicode characters this problem popped up. If i try to pass a unicode character (in this case im using the euro symbol) into any of the *wprintf functions it will fail, but seemingly only in xcode. The same code works fine in visual studio and I was even able to get a friend to test it successfully with gcc on linux. Here is the offending code:
wchar_t _teststring[10] = L"";
int _iRetVal = swprintf(_teststring, 10, L"A¥€");
wprintf(L"return: %d\n", _iRetVal);
// print values stored in string to check if anything got corrupted
for (int i=0; i<wcslen(_teststring); ++i) {
wprintf(L"%d: (%d)\n", i, _teststring[i]);
}
In xcode the call to swprintf will return -1, while in visual studio it will succeed and proceed to print out the correct values for each of the 3 chars (65, 165, 8364).
I have googled long and hard for solutions, one suggestion that has appeared a number of times is using a call such as:
setlocale(LC_CTYPE, "UTF-8");
I have tried various combinations of arguments with this function with no success, upon further investigation it appears to be returning null if i try to set the locale to any value other than the default "C".
I'm at a loss as to what else i can try to solve this problem, and the fact it works in other compilers/platforms just makes it all the more frustrating. Any help would be much appreciated!
EDIT:
Just thought i would add that when the swprintf call fails it sets an error code (92) which is defined as:
#define EILSEQ 92 /* Illegal byte sequence */
It should work if you fetch the locale from the environment:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void) {
setlocale(LC_ALL, "");
wchar_t _teststring[10] = L"";
int _iRetVal = swprintf(_teststring, 10, L"A¥€");
wprintf(L"return: %d\n", _iRetVal);
// print values stored in string to check if anything got corrupted
for (int i=0; i<wcslen(_teststring); ++i) {
wprintf(L"%d: (%d)\n", i, _teststring[i]);
}
}
On my OS X 10.6, this works as expected with GCC 4.2.1, but when compiled with CLang 1.6, it places the UTF-8 bytes in the result string.
I could also compile this with Xcode (using the standard C++ console application template), but because graphical applications on OS X don't have the required locale environment variables, it doesn't work in Xcode's console. On the other hand, it always works in the Terminal application.
You could also set the locale to en_US.UTF-8 (setlocale(LC_ALL, "en_US.UTF-8")), but that is non-portable. Depending on your goal there may be better alternatives to wsprintf.
If you are using Xcode 4+ make sure you have set an appropriate encoding for your files that contain your strings. You can find the encoding settings on a right pane under "Text Settings" group.
Microsoft had a plan to be compatible with other compilers starting from VS 2015 but finally it never happened because of problems with legacy code, see link.
Fortunately you can still enable ISO C (C99) standard in VS 2015 by adding _CRT_STDIO_ISO_WIDE_SPECIFIERS preprocessor macro. It is recommended while writing portable code.
I found that using "%S" (upper case) in the formatting string works.
"%s" is for 8-bit characters, and "%S" is for 16-bit or 32-bit characters.
See: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
I'm using Qt Creator 4.11, which uses Clang 10.