|
-
September 27th, 2002, 08:00 AM
#1
Dynamic char table
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
-
September 27th, 2002, 09:03 AM
#2
Try
map<string,string> lookup
One string can be the login, the other the password.
Succinct is verbose for terse
-
September 27th, 2002, 09:05 AM
#3
Hum I-m programming C Ansi?
What is that ? 
Do you have an example of C code pls ?
-
September 27th, 2002, 09:14 AM
#4
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:
Code:
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
-
September 27th, 2002, 09:22 AM
#5
That looks like a good beginning, Paul. It's things like this that make you really appreciate encapsulation.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
September 27th, 2002, 10:07 AM
#6
<grin> I'd have to agree with you there.
--Paul
-
September 27th, 2002, 11:30 AM
#7
Well thanx Paul !
Forgot to specify it was C sorry 
I'm gonna try this as soon as possible!
-
September 27th, 2002, 11:41 AM
#8
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 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
Last edited by tux0r; September 27th, 2002 at 11:51 AM.
-
September 27th, 2002, 01:14 PM
#9
Alright, man. I will post here. If you need any further assistance,
e-mail me at [email protected] [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].
Code:
#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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|