#include <qapplication.h>
#include <qmainwindow.h>
#include "mainwindow.hpp"
#include "../RegisterOfErrors.hpp"
#include <clocale>
extern std::string* Error::DescriptionOfErrors;
int main (int argc, char *argv[])
{
std::locale::global(std::locale("en_US"));
setlocale(LC_ALL, "en_US");
FILE *conf = fopen("dupa.txt", "r");
float dupa;
fscanf(conf, "%f", &dupa);
printf("%f\n", dupa);
Error::setDescriptionOfErrors();
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
My default locales are "es_ES", so "," is a decimal point.
It is my code. In the file "dupa.txt" is a number "1.0344" and it works correctly. However, deeper in the code I'm using the fann library, which is linked in g++ by "-ldoublefann" and read some data from files, and in this library works only ",".
The problem was caused by Qt.
There is some code
#include "doublefann.h"
#include "fann_cpp.h"
#include <clocale>
int main() {
setlocale(LC_ALL, "en_US");
const int max_neurons = 20;
const int neurons_between_reports = 1;
const double desired_error = 0.0001;
FANN::neural_net* repetition_ann;
repetition_ann = new FANN::neural_net();
repetition_ann->create_shortcut(2, 2, 1);
repetition_ann->cascadetrain_on_file("train.dat", max_neurons, neurons_between_reports, desired_error);
}
And this code works as I expect - It reads numbers, which have ".", from file "train.dat" ad prints numbers with ".".
The difference between those cases: in the first case the similiar code is somewhere in qtapplication, this code is independent.
Qt sets own locales, so the solution is adding a line: std::locale::global(std::locale("en_US")); and #include <QtCore>
Related
I'm attempting to learn the remove() function in C, and I want to make a program with first gets the environmental variable with getenv() function, then uses it inside the code.
However, I get the error
"too many arguments to function remove()".
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* a = getenv("USERPROFILE");
remove("%s/Desktop/remove.txt", a);
return 0;
}
If you are simply trying to combine them this should work.
I commented out some lines and put a printf so you can run it to see the result.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXPATH 256
int main()
{
//char* a = getenv("USERPROFILE");
char* a = "userprofile";
char path[MAXPATH];
strcpy(path,a);
strcat(path,"/Desktop/remove.txt");
//remove(path);
printf("PATH: %s",path);
return 0;
}
I want to implement RSA-Signature where I can calculate the signature of a file and verify a signature of a file. Therefore I use the libary gmp.
But when I want to print the data of the file, it always prints 0 even though the file is not empty. Here is my code:
//compiled with gcc rsa-sig.c -O3 -Wall -l gmp -o rsa-sig
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <gmp.h> /* GNU Multi Precision library */
int main(int argc, char *argv[])
{
int r;
mpz_t modulus;
mpz_init(modulus);
r=mpz_set_str(modulus, "FFFF",16); //just an example modulus
mpz_t data;
mpz_init(data);
FILE * stream;
stream = fopen("file.bin", "rb");
r= mpz_inp_str(data, stream,16);
mpz_mod(data,data,modulus);
gmp_printf("%Zd\n",data);
}
I can't figure out why the output of that is 0. Maybe one of you guys have an idea.
Thanks!!
I can't seem to get termcap's "cl" command to work, but the terminal escape code does.
For example:
#include <termcap.h>
#include <stdio.h>
int main()
{
tputs(tgetstr("cl", NULL), 1, putchar);
}
This doesn't change the terminal. But when I run:
#include <stdio.h>
int main()
{
printf("\e[2J");
}
or if I call echo `tput cl`
The terminal is cleared.
Why does this happen? Shouldn't termcap give that same escape code?
EDIT: Fixed writing characters
EDIT2: It's because i didn't call tgetent() before calling tgetstr(). Thanks guys!
Before interrogating with tgetstr(), you need to find the description of the user's terminal with tgetent():
#include <stdio.h>
#include <stdlib.h> // getenv
#include <termcap.h> // tgetent tgetstr
int main(void)
{
char buf[1024];
char *str;
tgetent(buf, getenv("TERM"));
str = tgetstr("cl", NULL);
fputs(str, stdout);
return 0;
}
Compile with -ltermcap
i use xcode to write a c command line program,but i found the jump to define is not work well for the stand lib,i record a picture to show what happen,is that normal with xcode?
//
// main.c
// 0.4 xCodecantjump
//
// Created by Sen on 14-5-19.
// Copyright (c) 2014年 SLboat. All rights reserved.
//
#include <stdio.h>
#include <limits.h>
#include "main.h"
int main(int argc, const char * argv[])
{
int a = INT_MAX;
int c = INT_MIN;
int b = START;
// insert code here...
printf("some int: %d, %d, %d\n", a, c ,b);
return 0;
}
I suspect it is some kind of ObjC optimization.
Switching to C++ Source compilation solved this problem (with rebuild).
Tested on regexpal.com this regex works fine, but when run in my program it doesn't work at all. The objective is to pull the video ID from the url (And throw an error if it's not a valid url)
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
regex_t expression;
char * regexpression = "^(https?://)?(www\\.)?youtube\\.com/watch\\?(.*&)?v=(.*?)(&.*)?$";
regcomp(&expression,regexpression,0);
char * url = "http://www.youtube.com/watch?v=HereBeVideoId";
if(regexec(&expression, url, 0, NULL, 0)){
printf("Url %s not a valid video.\n",url);
return;
}
return 0;
}
Add REG_EXTENDED flag to regcomp() function:
regcomp(&expression,regexpression,REG_EXTENDED);