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);
}
|
|
0
|
|
|
|
Reply
|
tiger (6)
|
4/28/2006 7:14:37 PM |
|
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.
|
|
0
|
|
|
|
Reply
|
cbmanica (39)
|
4/28/2006 9:07:39 PM
|
|
In article <445269b9$0$27869$626a54ce@news.free.fr>,
Tiger <tiger@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 entr�es 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)
|
|
0
|
|
|
|
Reply
|
roberson2 (8067)
|
4/28/2006 9:31:12 PM
|
|
|
2 Replies
34 Views
(page loaded in 0.066 seconds)
Similiar Articles: use of aio_read and aio_write - comp.unix.programmerhello all, I'm trying to copy a file using async io. /* async I/O structures */ struct aiocb cb; const struct aiocb* list[1] = {&cb}; int aioRe... How to read/write .mat files in VB.net... - comp.soft-sys.matlab ...No storage allocated, no data types, just text that gets put ... do that, you could also pull apart the struct and save ... How to read/write .mat files in VB.net ... Dot notation in struct - comp.lang.c... no type) found in a struct in a driver module: static const struct file_operations mycdrv_fops = { .owner = THIS_MODULE, .read = mycdrv_read, .write ... write read string data - comp.lang.java.helpHi All, I am attempting to write data into a file and read it back again. ... it to the original "data" > > At one point I even made up a structure that ... convert mat files (struct) to ascii text files / save data as ...How to read/write .mat files in VB.net... - comp.soft-sys.matlab ... convert mat files (struct) to ascii text files / save data as ... data format ... data i convert to ... IDL Error GCPC data - comp.lang.idl-pvwaveIDL> file = 'gpcp_v2.1_psg.1979' IDL> struct = Read_V2_1_File(file, HEADER=thisHeader ... code, and the modules are not named as I would name them if I were writing ... read labview files in matlab. - comp.soft-sys.matlabIs there a way to write the array in a format ... can put in a for loop at the begining to read a file in ... currentFileName = files(k).name; % extract file name from struct ... Reading an unformatted file - comp.lang.fortranIf you want to read/write some standardized file format, you have ... and understand your current file structure. 2. Make a short test program to write out a dummy file ... Best way to Read BMP image and write data (in Hex) to file - comp ...Hi all, I am looking for the best method to read in bitmap image data, convert that data to hex, and then write it to a file. Does anyone have... Sending an array of astruct over a TCP socket (Using C). - comp ...... over a TCP socket using an array of structures. I have ... with MSG_DONTWAIT and recv(2) instead of write(2) and read(2)! ... Problem sending binary file via socket. - comp ... Read and Write Structures to Files with .NET - CodeProjectReading and writing structure data to and from Binary files; Author: zadeveloper.com; Updated: 10 Aug 2005; Section: C#; Chapter: Languages; Updated: 10 Aug 2005 how to read and write a struct to file use C#? - Stack OverflowI can use write(&stName,sizeof(stName),&FileName) and define a same struct in other program to read the file(XXX.h) when i use C, But I want do the same use C# and I ... 7/13/2012 4:18:41 PM
|