write and read struct from file

T

Tiger

I try to write a struct into a brut file but I can't write all var in
this struct
when I try to add user I have that :
> testmachine:/usr/share/my_passwd# ./my_passwd -a users.db
> Ajout d'une entrée dans la base :
>
> Username : test
> Password : aze
> Gecos : qsd
> Home directory : wxc
> Shell : rfv
> Nb Items : 1

And when I try to list all data in my list, I have that
> testmachine:/usr/share/my_passwd# ./my_passwd -l users.db
> Liste toutes les entrées dans la base :
>
> Username : testg@Password : Gecos : ¼@¾«Lc@ØÄ@¼p@>@(x@À¨@¤öÿ¿¢B@ l@Äy@½@¬@¿
>
Home directory : <î@ðöÿ¿<{Shell : @

Can somebody help me ???

this is my struct :
typedef struct _Infos Infos;

struct _Infos {
char pw_name[255]; /* user name */
char pw_passwd[255]; /* password */
unsigned int pw_uid; /* user uid */
unsigned int pw_gid; /* user gid */
char pw_gecos[128]; /* general info */
char pw_dir[255]; /* home directory */
char pw_shell[64]; /* default shell */
};


this is my code :
#include "my_passwd.h"

/* Init base */
void init_userdb (char* file)
{
Infos user;
int f;

f = open(file, O_RDONLY|O_CREAT);
if (f < 0) exit(-1);

userdb = list_new();

while (read(f, &user, sizeof(user)) > 0) {
userdb = list_push(userdb, &user);
}

close(f);
}


/* save into base */
void save_userdb (char *file)
{
List *vListMove;
int f;

f = open(file, O_RDWR);
if (f < 0) exit(-1);

for(vListMove = userdb; !list_is_end(pList,vListMove);
list_move_next(userdb,vListMove)) {
write(f, vListMove->data, sizeof(vListMove->data));
}

close(f);
}


/* add to base */
void adduser (char *file)
{
Infos user;

init_userdb(file);

printf("Username : ");
fgets(user.pw_name, sizeof (user.pw_name), stdin);
printf("Password : ");
fgets(user.pw_passwd, sizeof (user.pw_passwd), stdin);
printf("Gecos : ");
fgets(user.pw_gecos, sizeof (user.pw_gecos), stdin);
printf("Home directory : ");
fgets(user.pw_dir, sizeof (user.pw_dir), stdin);
printf("Shell : ");
fgets(user.pw_shell, sizeof (user.pw_shell), stdin);
user.pw_uid = 0;
user.pw_gid = 0;

userdb = list_push(userdb, &user);

save_userdb (file);
printf("Nb Items : %d",list_nb_item(userdb));

printf("\n");
exit(0);
}


/* del item from la base */
void deluser (char *file)
{
puts("del item from la base.\n");
exit(0);
}


/* List all items */
void listusers (char *file)
{
List *vListMove;
Infos *user;

init_userdb(file);


for(vListMove = userdb; !list_is_end(pList,vListMove);
list_move_next(userdb,vListMove)) {
user = vListMove->data;
printf("Username : %s",user->pw_name);
printf("Password : %s",user->pw_passwd);
printf("Gecos : %s",user->pw_gecos);
printf("Home directory : %s",user->pw_dir);
printf("Shell : %s",user->pw_shell);
printf("\n");
}

exit(0);
}
 
C

cbmanica

Tiger said:
I try to write a struct into a brut file but I can't write all var in
this struct
when I try to add user I have that :
Can somebody help me ???

(snip non-program code)

It's a safe bet that the order in which you're trying to call things is
relevant to why things seem not to be working. Posting the smallest
*complete* program that demonstrates your problem is polite and much
more likely to get good results than code that assumes declarations and
definitions about which you've told us nothing, not to mention code
lacking a definition of main() which would make analysis more reliable.
 
W

Walter Roberson

I try to write a struct into a brut file but I can't write all var in
this struct
And when I try to list all data in my list, I have that

/* add to base */
void adduser (char *file)
printf("Username : ");
fgets(user.pw_name, sizeof (user.pw_name), stdin);

You need to fflush(stdout) if you expect the prompt to appear before
the data is read.

/* List all items */
void listusers (char *file)
{
List *vListMove;
Infos *user;
user = vListMove->data;

You do show us the declaration of user, as being a pointer to something,
but you do not show us the structure of List, so we do not know
what the type is of (*vListMove).data . If we hypothesize that
data is a struct rather than a pointer to a struct, then you would
be populating your pointer user with garbage and then attempting to
use it.
printf("Username : %s",user->pw_name);
printf("Password : %s",user->pw_passwd);

You probably want to seperate the fields on output,
rather than having them run all together. You probably want to
insert a space. If you are going to have all the printf() seperate
instead of combining them into one, it would likely make the most
sense to have that space right at the beginning of the string format
for the second and succeeding lines, such as " Password : %s" .
printf("Gecos : %s",user->pw_gecos);
printf("Home directory : %s",user->pw_dir);
printf("Shell : %s",user->pw_shell);
printf("\n");

Notice here that you -assume- that all the strings you get through
the struct will be properly null terminated. When you created the
data and stored it into the database the strings are indeed null
terminated, but it is better to practice safe programming by
checking first. In conjunction with this, you might find it easiest
to modify your input routines to zero the fields before you fgets()
into them: if you do that, then you will know that no matter how
long the string actually stored, that the last character of the
array will be a null character. Then the sanity check on
output can be just to check the last character of the buffer for
the null character: if it is not there then the array is
certainly trashed, and if it is there, then the array might still
be trash but at least you're sure that the %s format would not
run off into the next object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top