473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Apr 28 '06 #1
2 3660
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 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)
Apr 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas | last post by:
What's the quickest way to write and read 10.000 integer values ( or more ) to and from a file? Using struct somehow? The example in the docs shows how to handle to or three arguments, but is the...
3
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw,...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
2
by: stephen fx | last post by:
Hello all! Using C/C++ I can do this: struct MyStruct { int a; char b; }; MyStruct test;
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
1
by: =?Utf-8?B?RGVubmlz?= | last post by:
I have a client socket connection (Linux) and a server socket connection (Windows). All is fine with the sockets themselves. The client-side is sending data (struct) to the server via write()....
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.