My C programming is decidedly rusty:-(
I have studied http://zetcode.com/db/mysqlc/ and essentially comfortable with it.
But I am unsure of the best way of storing the data retrieved.
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
for(int i = 0; i < num_fields; ) {
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
this works fine and give me exactly the correct results.
Being familiar with perl hashes, I am tempted to use a C structure. Each record in my (small) database consists of different types of columns: char, float, int etc.
So I have:
struct Parameter {
char name[LEN_NAME];
float max;
float min;
float sample_interval;
int active;
char units[LEN_UNITS];
char start[LEN_START];
char start_time[LEN_START_TIME];
char end[LEN_END];
char end_time[LEN_END_TIME];
char type[LEN_TYPE];
};
struct Parameter parameters[MAX_PARAMETERS];
Where the LENs are defined constants.
I am trying things like:
num_fields = mysql_num_fields(result);
int r = 0;
while ((row = mysql_fetch_row(result))) {
for(int i = 0; i < num_fields; i++) {
printf("%s ", row[i] ? row[i] : "NULL");
sscanf(row[0], "%LEN_NAMEs", parameters[r].name);
sscanf(row[1], "%d", ¶meters[r].max);
Which seems to be working OK for the chars, but not for the other types.
Struggling. Perhaps I am up a gum tree!
Related
I have an exercise and I don't know how to solve it well!
I want write a C program that give from user the information of a student and then save it to file A.txt. After that reverse the first name, last name and student number and save it to file B.txt.
For example:
john
lopez
123456
It changes to:
nhoj
zepol
654321
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
struct student {
char fname[SIZE];
char lname[SIZE];
char num[SIZE];
}st;
int main()
{
FILE *in, *out;
char ch;
int tmp=0,flag=0,i;
printf("INPUT First name: ");
scanf("%s", &st.fname);
printf("INPUT Last name: ");
scanf("%s", &st.lname);
printf("INPUT personal num: ");
scanf("%s", &st.num);
in= fopen("A.txt", "w");
fputs(st.fname, in);
fputs(st.lname, in);
fputs(st.num, in);
fclose(in);
in= fopen("A.txt", "r");
out=fopen("B.txt", "w");
fgets(st.fname, strlen(st.fname)+1,in);
strrev(st.fname);
fputs(st.fname, out);
printf("\n%s", st.fname);
fgets(st.lname, strlen(st.lname)+1, in);
strrev(st.lname);
fputs(st.lname, out);
printf("\n%s", st.lname);
fgets(st.num, strlen(st.num)+1, in);
strrev(&st.num);
fputs(st.num, out);
printf("\n%d", st.num);
fclose(in);
fclose(out);
return 0;
}
If you want to copy the data to a file, then you first need to put the data in some dynamic memory allocation and then after reversing the content required in dynamic memory, you need to copy it into your file.
this is the program to reverse the each word in given string
#include
#include
void reverse_string(char*);
void reverse_words(char*);
int main() {
char a[100];
gets(a);
reverse_words(a);
printf("%s\n", a);
return 0;
}
void reverse_words(char *s) {
char b[100], *t, *z;
int c = 0;
t = s;
while(*t) { //processing complete string
while(*t != ' ' && *t != '\0') { //extracting word from string
b[c] = *t;
t++;
c++;
}
b[c] = '\0';
c = 0;
reverse_string(b); // reverse the extracted word
z = b;
while (*z) { //copying the reversed word into original string
*s = *z;
z++;
s++;
}
while (*s == ' ') { // skipping space(s)
s++;
}
/*
* You may use if statement in place of while loop if
* you are assuming only one space between words. If condition is
* used because null terminator can also occur after a word, in
* that case we don't want to increment pointer.
* if (*s == ' ') {
* s++;
* }
*/
t = s; // pointing to next word
}
}
/*
* Function to reverse a word.
*/
void reverse_string(char *t) {
int l, c;
char *e, s;
l = strlen(t);
e = t + l - 1;
for (c = 0; c < l/2; c++) {
s = *t;
*t = *e;
*e = s;
t++;
e--;
}
}
Then you can try using fputc, i.e., reading char by char along with a loop to get your data line by line as 3 lines.
char *ch;
for(i=0;st.fname[i];i++)
{
ch=getc(st.fname[i]);
fputc(ch,in);
}
repeat the same even for st.lname and st.num
I would like to know what techniques I could apply to add some dimensions of an array and save to a new Vet lower as in the following example:
A -> [1,2], [3,4], [5,6]
B -> [3], [7], [11]
figure:
http://snag.gy/83Qwl.jpg
If you want to write your own CUDA kernel, take a look at the Vector add sample. Instead of passing 2 input vectors to the kernel, you would pass just A and provide a loop to sum over the "rows" of A:
__global__ void mykernel(int *A, int *B, int rows, int cols){
int idx=threadIdx.x+blockDim.x*blockIdx.x;
if (idx < rows) {
int sum = 0;
for (int i=0; i< cols; i++)
sum += A[(idx*cols)+i];
B[idx] = sum;
}
}
This won't be terribly efficient, but you can improve the efficiency if you can store your A array in column major order:
A -> [1,3,5], [2,4,6]
then a modification to the above kernel becomes pretty efficient:
__global__ void mykernel(int *A, int *B, int rows, int cols){
int idx=threadIdx.x+blockDim.x*blockIdx.x;
if (idx < rows) {
int sum = 0;
for (int i=0; i< cols; i++)
sum += A[(i*cols)+idx];
B[idx] = sum;
}
}
If you're looking for efficiency but can't reorganize your data, then a segmented parallel reduction will be fastest. You can try creating something based on the cuda sample codes but I would suggest using thrust, specifically reduce_by_key
You would leave your A array as is and use it as the "values":
A -> [1,2], [3,4], [5,6]
And you would create a "key" array which corresponds to the rows of your A array:
K -> [0,0], [1,1], [2,2]
help please with Hunspell problem, can\t make this stuff works well.
Here is my code, it returns bool value TRUE unstead the suggestion of word.
int main(array<System::String ^> ^args)
{
char *aff = "c:\\en_US.aff";
char *dic = "c:\\en_US.dic";
Hunspell *spellObj = new Hunspell(aff,dic);
const char *named = "hello";
int result = spellObj->spell(named);
char ** wlst;
char ** wlst2;
int ns = spellObj->suggest(&wlst,named);
int abc = spellObj->analyze(&wlst2,named);
Console::WriteLine(ns);
for (int i = 0; i<ns; i++)
{
Console::WriteLine(&wlst[i]);
}
spellObj->free_list(&wlst,ns);
delete spellObj;
Console::WriteLine(result);
getchar();
return 0;
How can i make this suggestion works?
must use std::cout instead Console::WriteLine (facepalm)
I am using the thrust partition function to partition array into even and odd numbers. However, when i try to display the device vector, it shows random values. Please let me know where is the error. I think i have done everything correct.
#include<stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include<thrust/partition.h>
struct is_even
{
//const int toCom;
//is_even(int val):toCom(val){}
__device__
bool operator()(const int &x)
{
return x%2;
}
};
void main(){
thrust::host_vector<int> H(6);
for(int i =0 ; i<H.size();i++){
H[i] = i+1;
}
thrust::device_vector<int> D = H;
thrust::partition(D.begin(),D.end(),is_even());
for(int i =0 ;i< D.size();i++){
printf("%d,",D[i]);
}
getchar();
}
You can't send a thrust::device_reference (i.e., the result of D[i]) through printf's ellipsis because it is not a POD type. See the documentation. Your code will produce a compiler warning to this effect.
Cast to int first:
for(int i = 0; i < D.size(); ++i)
{
printf("%d,", (int) D[i]);
}
Any idea on how to save an array of unsigned char to a field in MySQL database ? I'm considering BLOB, but how can I convert it to QByteArray and convert it back to an array of unsigned char when I want to ? BTW, I'm trying to save fingerprint data.
I managed to solve this using the codes below (copied directly from my project)
struct fp {
unsigned char Template[MAX_TEMPLATE_SIZE];
int size;
};
// unsigned char to QByteArray
QByteArray FingerPrint::charArrayToByteArray(fp fp0) {
QByteArray ba;
for (int i=0; i
// QByteArray to unsigned char
fp FingerPrint::byteToFp(QByteArray *ba) {
fp fp0;
for (int i=0; isize(); i++) {
fp0.Template[i] = ba->at(i);
}
fp0.size = ba->size();
return fp0;
}