468,170 Members | 1,945 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

write and read struct from file

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 entre 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 entres 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);
}

Apr 28 '06 #1
2 3280
Tiger wrote:
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.

Apr 28 '06 #2
In article <44***********************@news.free.fr>,
Tiger <ti***@hotmail.com> wrote:
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
testmachine:/usr/share/my_passwd# ./my_passwd -l users.db
Liste toutes les entres dans la base :

Username : testg@Password : Gecos : @Lc@@p@>@(x@@B@


/* 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.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Apr 28 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Thomas | last post: by
8 posts views Thread by a | last post: by
24 posts views Thread by Bill | last post: by
1 post views Thread by =?Utf-8?B?RGVubmlz?= | last post: by
63 posts views Thread by Bill Cunningham | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.