I'm trying to read an xml file's attributes where I'm storing information. I need to retrieve that information in the form of strings and I'm unable to do that since I cant find any tinyXml method that would return a string and I cant figure out a way to convert things from char* to String^. Does anyone have a suggestion on what to do?
Thanks
In TinyXML you can get XmlElements attribute with Attribute() function. It returns const char * which can be converted to string as Adrian have stated in his/her answer according to http://cplusplus.com/reference/string/string/string/ with string ( const char * s ).
More about the Attribute function: http://www.grinninglizard.com/tinyxmldocs/classTiXmlElement.html#e419a442a9701a62b0c3d8fd1cbdd12d
You can convert char * to string.
According to http://cplusplus.com/reference/string/string/string/ C++ string has this ctor: string ( const char * s )
Related
In a previous question about using CStrings to create table data in an SQL extension, it was a problem to use Datums made from CStringGetDatum() for table columns that expect VARCHAR. The solution was to use CStringGetTextDatum(). Now i am curious why.
Here are the function definitions, but i am not sure, in which situation to use CStringGetDatum() over the second, if you can't use the first with CStrings:
#define CStringGetDatum(X) PointerGetDatum(X)
#define CStringGetTextDatum(s) PointerGetDatum(cstring_to_text(s))
#define PointerGetDatum(X) ((Datum) (X))
text *
cstring_to_text(const char *s)
{
return cstring_to_text_with_len(s, strlen(s));
}
text *
cstring_to_text_with_len(const char *s, int len)
{
text *result = (text *) palloc(len + VARHDRSZ);
SET_VARSIZE(result, len + VARHDRSZ);
memcpy(VARDATA(result), s, len);
return result;
}
The data type text or varchar is not stored as a zero-terminated array of characters. It is a varlena, which has the length (and other stuff) stored in the beginning.
The data type cstring is for a C string and is an internal data type in PostgreSQL. You can never use it in SQL.
Use CStringGetDatum whenever you need to pass a Datum that is a C string and use CStringGetTextDatum to convert a C string to a text or varchar that you need to pass as a Datum.
Thank you, it works ....................................................................
strcpy(holder[pos].key, new.key);
holder[pos].position = new.position
strcpy(holder[pos].name, new.name);
strcpy(holder[pos].country, new.country);
You don't need strcpy or any special function for that. Strcopy is only necessary because strings aren't strictly just a data type in C, but null-terminated char arrays.
You can do it like this:
holder[pos].position = new.position; // Assigns the value of new.position to holder[pos].position
please use
holder[pos].position = new.position
I want to know how to use gettext in C with dynamic message id in the argument.
For example,
char *var = fun(); //where fun returns char * type.
char *val = gettext(var);
Your code is fine, but you will of course also need to make sure that fun() returns a static string, that's going to stick around. Also val should be const char *, you cannot change the strings owned by gettext.
Im reading from dev/random and storing the binary data into an array.
When I write the array into my log its displayed as I want:
DebugLogMsg10 (pDebugLogger, sizeThreadID, "login failed->%s", UserSession.szToken);
But when I put it into my page build function call (the second parameter is expecting char * as pointer to the string which I want to be set as cookie.
PutMainLogIn (UsersAuthentication, UserSession.szToken, &requestFcgx);
If I do for example:
PutMainLogIn (UsersAuthentication, "testtest", &requestFcgx);
It is also working fine and a cookie with the value testtest is set.
So What is the problem?
Is it not allowed to set binary data as cookie? Or Am I missunderstanding something?
ps:
this is the structure looking like:
typedef struct Sesseion_s
{
size_t sizeID;
char szToken[TOKEN_LEN];
} Sesseion_t;
And this is the function prototype:
void PutMainLogIn (UsersAuth_t pUserData, char *szToken, FCGX_Request *Fcgx_Request)
I need to create mini social network in C. I made a struct for each person. And I'm storing each friend in a char pointer with id's. I thought I can manage it like this:
ptr_friends = id1,id2,id3,id4...
and when I need them I could just read those by using strtok.
But I couldn't manage to save it that way. It should be like this:
ptr_friends = ptr_friends + id + ","
but of course it doesn't work this why and I don't know how to do it.
How can I save and use this way? Or if you have another idea for a save method please tell.
I'd suggest that you make ptr_friends a pointer to multiple chars by using malloc(size_t) and then resizing the space with realloc(void *, size_t) everytime you want to add an ID to the friendlist. That way you can just get the numbers using ptr_friends[i].
For example :
int friends_size = 1;
char *ptr_friends = malloc((size_t)1);
ptr_friends[0] = john_id; // john_id is a fictional ID here
And when you want to add a friend :
ptr_friends = realloc(ptr_friends, ++friends_size);
ptr_friends[friends_size-1] = mary_id;
EDIT :
If you want to make a function to add a friend, for example addfriend(char *,int), doing the following is an error :
void addfriend(char *ptr_friends, int *friends_size, int id)
{
ptr_friends = realloc(ptr_friends, (size_t) ++(*friends_size));
ptr_friends[friends_size-1] = id;
}
ptr_friends here is getting reallocated, and since the pointer can move while being reallocated, we're storing it in ptr_friends. But, it's the ptr_friends from inside the function, that means that the pointer we give to the function will not get modified, since arguments to a function are copied elsewhere beforehand. That means that you have to give a pointer to the pointer, so you can modify the pointer in the main code.
I guess this will help you : https://stackoverflow.com/a/5901241/2549281
You should use something like :
strcat(ptr_friends, id);
strcat(ptr_friends, ",");
instead of ptr_friends + id + ","
Would not it be easier to you to do an array of ints with ids?
int max_friends = 50;
int friends_size = 0;
int friends* = malloc(sizeof(int) * MAX_FRIENDS);
friends[friens_size] = id;
friends_size++;
Another suggestion is instead of using a string or a simple array you can use a "generic" dynamic array (using void*). It's a simple code and you can find many examples.
take a look at that question: C dynamically growing array