wchar_t strings not working C - c

I'm trying to create a program that needs to get string input from the user, since it's has to work with portuguese words i'm using wchar_t,the problem is,
C seems to have a will, because when i need it to work it doens't but then out of the blue it works in some simple test.
the following code worked 5 minutes ago and now it doesn't:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
/
int main(){
setlocale(LC_ALL,"Portuguese");
wchar_t meu[3];
fgetws(meu,3,stdin);
fputws(meu,stdout);
return 0;
}

Related

Why is #include <stdio.h> is underlined in red?

I have this basic code in C. A program to say 'Hello World'. And the first line That say #include <stdio.h> is underlined in red as though an error is occurred.
#include<stdio.h>
main()
{
printf("hello, world\n")
}
I'm using vs code to run the program, is it a bug in vs code? or maybe a problem in the installation of C?
please help.
I don't understand where the problem originates so no actions have been pursued.
Add space between #include and <stdio.h>
Also, specify the return type of your main function.
Also at the end of printf("hello, world\n"), there should be semi colon
#include <stdio.h>
void main(){
printf("hello, world\n");
}
it is void since it is not returning anything.

C++ vector.assign (contents of char array) works in WinXP-32, fails in Win10-64; why?

On this StackExchange topic, first answer and last comment upon that answer, I learned to assign the contents of my char array (which I just read from file) into my vector. The following code works fine in Windows XP, 32-bit, Visual Studio 2010, but fails in Win10, 64-bit, Studio 2012. Both projects use the Unicode character set. The contents of the file myConfig.txt are (separated by tabs):
words 3 mobius lagrange gauss
I am a complete noob, so if some mistake seems too stupid for anyone to make, go ahead and assume I made it.
The code:
#include "stdafx.h"
#include <windows.h>
#include <vector>
#include <iterator>
#include <string.h>
#include <wchar.h>
#include <tchar.h>
using namespace std;
vector<wchar_t> wvec;
int n;
wchar_t ss[256];
FILE* pfile;
int _tmain(int argc, _TCHAR* argv[])
{
fopen_s(&pfile,"myConfig.txt","r");
fwscanf_s(pfile,L"%ls",&ss);
wprintf(L"var name is %ls\n",ss);
fwscanf_s(pfile,L"%d",&n);
printf("num words is %d\n",n);
for (int i=0; i<3; i++) {
fwscanf_s(pfile,L"%ls",&ss);
wvec.clear();
n=wcslen(ss);
wprintf(L"vec empty %ls length %d\n",vec,vec.size());
wvec.assign(ss,ss+n+1); // +1 to contain null char
wprintf(L"vec sz %d filled %ls\n",wvec.size(),wvec);
}
printf("press Enter to finish\n");
getchar();
return 0;
}
On the Win10 machine, the output says, in part, "vec sz 7 filled ???", when I run/debug from the development environment. When I run the exe in the x64\Release folder, the corresponding line of output says "vec sz 5 filled ???", while the contents of myConfig.txt are exactly the same.
On the XP machine, the output is perfect.
Finally answered my own question: RTFM. It should not be
fscanf_s(pfile,"%s",&s);
It should be
fscanf_s(pfile,"%s",s, _countof(s));
I dunno why it worked in XP (convenient #bytes/char?) but, whatever.

Printing a unicode box in C

I'm trying to print this medium shade unicode box in C: ▒
(I'm doing the exercises in K&R and then got sidetracked on the one about making a histogram...). I know my unix term (Mac OSX) can display the box because I saved a text file with the box, and used cat textfilewithblock and it printed the block.
So far I initially tried:
#include <stdio.h>
#include <wchar.h>
int main(){
wprintf(L"▒\n");
return 0;
}
and nothing printed
iMac-2$ ./a.out
iMac-2:clang vik$
I did a search and found this: unicode hello world for C?
And it seems like I still have to set a locale (even though the executing environment in utf8? I'm still trying to figure out why this step is necessary) But anyway, it works! (after a bit of a struggle finally realizing that the proper string was en_US.UTF-8 rather than en_US.utf8 which I had read somewhere...)
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(){
setlocale (LC_ALL, "en_US.UTF-8");
wprintf(L"▒\n");
return 0;
}
Output is as follows:
iMac-2$ ./a.out
▒
iMac-2$
But when I try the following code...putting in the UTF-8 hex (which I got from here: http://www.utf8-chartable.de/unicode-utf8-table.pl?start=9472&unicodeinhtml=dec ) which is 0xe29692 for the box rather than pasting the box in itself, it doesn't work again.
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(){
setlocale (LC_ALL, "en_US.UTF-8");
wchar_t box = 0xe29692;
wprintf(L"%lc\n", box);
return 0;
}
I'm clearly missing something but can't quite figure out what it is.
The unicode value of the MEDIUM SHADE code point is not 0xe29692, it is 0x2592. <E2><96><92> is the 3 byte encoding for this code point in UTF-8.
You can print this thing either using the wide char APIs:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void) {
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t box = 0x2592;
wprintf(L"%lc\n", box); // or simply printf("%lc\n", box);
return 0;
}
Or simply by printing the UTF-8 encoding directly:
#include <stdio.h>
int main(void) {
printf("\xE2\x96\x92\n");
return 0;
}
Or if your text editor encodes the source file in UTF-8:
#include <stdio.h>
int main(void) {
printf("▒\n");
return 0;
}
But be aware that this will not work: putchar('▒');
Also for full unicode support and a few more goodies, I recommend using iTerm2 on MacOS.
The box character is U+2592, which translates to 0xE2 0x96 0x92 in UTF-8. This adaptation of your third program mostly works for me:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void)
{
setlocale (LC_ALL, "en_US.UTF-8");
wchar_t box = 0xe29692;
wprintf(L"%lc\n", box);
wprintf(L"\n\nX\n\n");
box = L'\u2592'; //0xE2 0x96 0x92 = U+2592
wprintf(L"%lc\n", box);
wprintf(L"\n\n0x%.8X\n\n", box);
box = 0x2592;
wprintf(L"%lc\n", box);
return 0;
}
The output I get is:
X
▒
0x00002592
▒
The first print operation produces nothing of use; the others work.
Testing on Mac OS X 10.10.5. I happen to be compiling with GCC 5.3.0 (which I compiled), but I got the same output with XCode 7.0.2 and clang.

cant get libsensors to work properly

here is my code concerning libsensors.
libraries:
#include <unistd.h>
#include <sensors/sensors.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
code concerning libsensors:
char sd[16384]="^\0",bf[1];
char buf2[8192]="^\0";
sensors_chip_name const* scn;
int c=0;
int t4=1;
while((scn=sensors_get_detected_chips(0,&c))!=0)
{
sensors_feature const *fea;
int f=0;
strcat(sd,scn->prefix);
printf("%s",scn->prefix);
strcat(sd,":");
strcat(sd,scn->path);
strcat(sd,"(");
while((fea=sensors_get_features(scn,&f))!=0)
{
strcat(sd,fea->name);
strcat(sd,"(");
sensors_subfeature const *sb;
int s=0;
while((sb=sensors_get_all_subfeatures(scn,fea,&s))!=0)
{
t4++;
strcat(sd,sb->name);
strcat(sd,",");
int t3=-1;
int i=0;
char t8[sizeof(sb->number)];
memcpy(&t8,&(sb->number),sizeof(sb->number));
strcat(sd,t8);
strcat(sd,"!");
}
strcat(sd,")");
}
strcat(sd,")");
}
so when I try to print anything nothing happens. char array called sd returns empty. it simply seems that there are no sensors to be read.
when I run sensors from terminal it works perfectly fine. I see a couple of cores and chips temps.
I implemented this code from some post on here and to be frank I don't totally understand it.
Posting #user3629249 comment as a community answer.
It it required to first call sensors_init() otherwise the chips list will be empty.
This function expects a sensors configuration file as argument, or NULL to use the default one.
Also, you can find an usage example in this related question: Has anyone been able to use libsensors properly?

C-Checking if input (float) is purely integer or float

I want to check if the user input is purely integer or a float. I attempted to do this by using floor and ceilfand comparing the values to the original x value in a function. However, this seems to be a tad bit problematic as the function keeps returning 0 instead of 1 for certain numbers like 5.5, when floor(5.5)!=5.5 and ceilf(5.5)!=5.5. This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>
int intchecker(float x)//in a separate file
{
if (floor(x)==x && ceilf(x)==x)
{
//printf("%f",floor(x));
return 0;
}
else {
return 1;
}
}
int main()
{
char line[] = " +----+----+----+----+----+----+----+----+----+----+---+";
char numbers[] = " 0 5 10 15 20 25 30 35 40 45 50";
float balls,slots;
int slot[9];
printf("==========================================================\nGalton Box Simulation Machine\n==========================================================\n");
printf("Enter the number of balls [5-100]: ");
scanf("%f",& balls);
if (balls>100 || balls<5){
printf("/nInput is not within the range. Please try again.");
}
else if (intchecker(balls)==1){
printf("/nInput is not an integer. Please try again.");
}
else {
printf(" This is an integer.");
//some more code here
}
}
I tried placing just the intchecker code in another project, which seems to work fine without any bugs, unlike in the previous project, where when I used a printf statement to check if the floor(x) value was correct, it kept showing different answers, e.g. "-2.000000" when input was 5.2. This is my code for the second project:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float x;
scanf("%f",&x);
if (floor(x)==x && ceilf(x)==x){
printf("Integer");
return 0;
}
else {
printf("Non-Integer");
return 1;
}
}
How is it possible that the second code works perfectly when the first code does not? Is something wrong with my way of writing/calling the function?(I am relatively new to functions-only 2 weeks of exposure so far)
I searched online and have seen many answers to check if input is integer or float, even on stackoverflow.com itself, but my wish is not to find out other ways to check if input is integer or float (for if I wished to do that, I could just google it, and there are many such questions on stackoverflow.com as well), but to comprehend why my first code does not work, for, as far as I know, it ought to work well without any of the bugs it is currently facing.
Any help is greatly appreciated!:)
Assuming a missing function declaration:
main.c is missing the prototype for int intchecker(float x) so main.c assumes the old-school prototype of int intchecker(int x) and the code exhibits undefined behavior. Anything could happen.
Add prototype in main.c or put it in separate.h and include that header file here and in separate.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <stdbool.h>
int intchecker(float x);
int main(void) {
...

Resources