Click to See Complete Forum and Search --> : Dynamic char table
tux0r
September 27th, 2002, 08:00 AM
Hi,
I did a quick search on the forums but didn't find what I'm looking for.
I want to create a char type table.
I have to put an unknown number of login and password in this table.
How do I do this ?
I never worked with dynamic char table .. only with int and it was really simple.
Thanks
cup
September 27th, 2002, 09:03 AM
Try
map<string,string> lookup
One string can be the login, the other the password.
tux0r
September 27th, 2002, 09:05 AM
Hum I-m programming C Ansi?
What is that ? :)
Do you have an example of C code pls ?
PaulWendt
September 27th, 2002, 09:14 AM
std::map is a part of the C++ standard library. See, when you
post a question on a C++ forum, expect to receive a C++ answer
unless you specify up-front that you need a C answer.
If you need a dynamic string table in C, you could do something
like the following:
typedef struct tagUSERNAME
{
char* pUsername;
char* pPassword;
} USERNAME;
USERNAME* createUser(const char* pUsername,
const char* pPassword)
{
USERNAME* pUser = malloc(sizeof(struct USERNAME));
pUser->pUsername = malloc(strlen(pUsername) + 1);
pUser->pPassword = malloc(strlen(pPassword) + 1);
strcpy(pUser->pUsername, pUsername);
strcpy(pUser->pPassword, pPassword);
}
void destroyUser(USERNAME* pUser)
{
free(pUser->pUsername);
free(pUser->pPassword);
free(pUser);
}
Then, you can create a dynamic array of pointers to those
structures. I might have screwed up some syntax or something,
but the overall idea is simple. I don't have anything to give you,
though; I'm not sure if you're looking for HOW to do it or for
a packaged solution.
--Paul
Graham
September 27th, 2002, 09:22 AM
That looks like a good beginning, Paul. It's things like this that make you really appreciate encapsulation. :)
PaulWendt
September 27th, 2002, 10:07 AM
<grin> I'd have to agree with you there.
--Paul
tux0r
September 27th, 2002, 11:30 AM
Well thanx Paul !
Forgot to specify it was C sorry :)
I'm gonna try this as soon as possible!
tux0r
September 27th, 2002, 11:41 AM
Then, you can create a dynamic array of pointers to those
structures. I might have screwed up some syntax or something,
but the overall idea is simple. I don't have anything to give you,
though; I'm not sure if you're looking for HOW to do it or for
a packaged solution.
Actually I'm not a C guru.
I have been coding in C for some years now (just at school and stuff) and I don't really know those things :)
Little bit more help would be really appreciated!
My code can be seen HERE (http://www.swissgeeks.com/actual_code.txt) if it helps !
Now I'd like to be able to add more than one user :)
And also list 'em,
Thanx for your help !
Best Regards,
Pierrick
PaulWendt
September 27th, 2002, 01:14 PM
Alright, man. I will post here. If you need any further assistance,
e-mail me at pwendt@NOSPAM.wideopenwest.com [remove the
NOSPAM.] part to contact me. There's no need to further clutter
up this forum [as this question is kind of off-topic anyway].
#include <stdio.h>
#define BUF_SZ 1024
/* functinos for per-user utilities */
/* these should go in one module */
typedef struct tagUSER
{
char* pUsername;
char* pPassword;
} USER;
USER* createUser(const char* pUsername, const char* pPassword)
{
USER* pUser = (USER*)malloc(sizeof(struct tagUSER));
pUser->pUsername = (char*)malloc(strlen(pUsername) + 1);
pUser->pPassword = (char*)malloc(strlen(pPassword) + 1);
strcpy(pUser->pUsername, pUsername);
strcpy(pUser->pPassword, pPassword);
return pUser;
}
void destroyUser(USER* pUser)
{
free(pUser->pUsername);
free(pUser->pPassword);
free(pUser);
}
/* functions for the concept of a user list */
/* these should go in another module */
typedef struct tagUSERLIST
{
unsigned int numUsers;
USER** ppUsers;
} USERLIST;
void initUserlist(USERLIST* pUserlist)
{
memset(pUserlist, 0, sizeof(USERLIST));
}
void addUser(USERLIST* pUserlist, USER* pUser)
{
pUserlist->ppUsers = (USER**)realloc(pUserlist->ppUsers, (pUserlist->numUsers+1) * sizeof(USER*));
pUserlist->ppUsers[pUserlist->numUsers++] = pUser;
}
USER* getUser(USERLIST* pUserlist, unsigned int index)
{
return (pUserlist->ppUsers[index]);
}
unsigned int getNumUsers(USERLIST* pUserlist)
{
return (pUserlist->numUsers);
}
void destroyUserlist(USERLIST* pUserlist)
{
for (unsigned int i = 0; i < pUserlist->numUsers; ++i)
{
destroyUser(pUserlist->ppUsers[i]);
}
free(pUserlist->ppUsers);
pUserlist->numUsers = 0;
}
/* main function. put this in its own module */
int main (int argc, char *argv[])
{
/*
char *pch1;
char *pch2;
char *pch3;
*/
/* i see lots of malloc()s in your code, but no free()s. have you */
/* ever heard of memory leaks? you must have a free() for every */
/* single malloc() that you call. */
/* also, these could all be stack variables: */
/* char info_login[BUF_SZ]; */
/* char *info_login = (char *) malloc (BUF_SZ); */
/* char *info_passw = (char *) malloc (BUF_SZ); */
/* char *parse = (char *) malloc (BUF_SZ); */
/* don't use 7 and 11; use strlen("LOGIN1=") or better yet, */
/* set up a #define for "LOGIN1=" and use strlen of that. */
/* strncpy(info_login, pch1 + 7, pch2 - pch1 - 7 ); */
/* strncpy(info_passw, pch2 + 11, pch3 - pch2 - 11 ); */
USERLIST userList;
initUserlist(&userList);
/* add two users */
addUser(&userList, createUser("DarkHelmet", "1234"));
addUser(&userList, createUser("testUser", "testPassword"));
/* print all users in the user-list */
for (unsigned int i = 0; i < getNumUsers(&userList); ++i)
{
USER* pUser = getUser(&userList, i);
printf("Name: %s, Password: %s\n", pUser->pUsername, pUser->pPassword);
}
destroyUserlist(&userList);
return 0;
}
--Paul
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.