Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

write and read struct from file

4 réponses
Avatar
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);
}

4 réponses

Avatar
Charlie Gordon
"Tiger" wrote in message
news:44526be0$0$27869$
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 : : 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 */
};


Wrong forum, we use French here...

Your problem may come from an alignment issue : pw_uid is most likely aligned on
a 32 bit boundary in memory whereas the file layout seems to expect it at offset
510 in the structure.
You may also be facing an endianness issue : writing in-memory structures
directly to disk is a problem if the expected endianness of the file format does
not match that of the CPU.
The size of type unsigned int depends on the architecture and the compiler,
check what the file format requires for fields pw_uid and pw_gid, possibly 16
bits values.
Once you investigate the above, you will know what to write to the file and will
do so one field at a time in the correct format.

Chqrlie.


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




Avatar
Stephane Legras-Decussy
"Tiger" a écrit dans le message de news:
44526be0$0$27869$
I try to write a struct into a brut file but I can't write all var in this
struct


il ne faut pas faire ça.
tu dois définir un format de fichier précis
et écrire et relire octet par octet.

Avatar
Tiger
"Tiger" a écrit dans le message de news:
44526be0$0$27869$
I try to write a struct into a brut file but I can't write all var in this
struct


il ne faut pas faire ça.
tu dois définir un format de fichier précis
et écrire et relire octet par octet.



Merci pour la réponse.

t'aurais un petit exemple par hazard
ce que je ne comprends pas, c'est ques les premières variables sont bien
enregistrées et pour le reste, il me met n'importe quoi.
J'ai cherché sur le net mais g pas réussi à trouver quelqu'un qui a eu
le mm pb
Tiger


Avatar
Stephane Legras-Decussy
"Tiger" a écrit dans le message de news:
445535e3$0$26751$
t'aurais un petit exemple par hazard
ce que je ne comprends pas, c'est ques les premières variables sont bien
enregistrées et pour le reste, il me met n'importe quoi.


c'est pas trivial comme boulot.
tu dois faire une fonction d'ecriture
de char, une d'ecriture de tableau
de char (qui utilise la precedente)
et une d'ecriture d'unsigned int.

ensuite tu develloppes "manuellement"
l'ecriture de la struct champ par champ...
operation inverse pour la lecture.

c'est fastidieux mais tu es certain de n'avoir
aucun problème.

de plus ta struct contient du binaire (les unsigned)
donc il faut ouvrir le fichier en mode binaire :
mode rb ou wb de fopen() ...


PS: open, read, write ... ne sont pas portables. prends
fopen, fread, fwrite ... voir la doc...