|
-
Dec 9th, 2003, 04:55 PM
#1
Thread Starter
<?="Moderator"?>
write struct to file
hey im trying to store data in a file from a struct? can anyone point me to how i should go about doing this, i tried like this but it says that cant convert my struct to char *
Code:
struct user_data{
char username[25];
char password[25];
};
......
ofstream file(USER_DATA,ios::out|ios::binary);
file.write(ud,sizeof(ud));
file.close();
can someone please point me on how i should do this thanks.
-
Dec 10th, 2003, 03:16 AM
#2
Hyperactive Member
u can do a memcpy to a character array.
-
Dec 10th, 2003, 02:41 PM
#3
Monday Morning Lunatic
Code:
file.write((char*)&ud, sizeof(ud));
Need to pass the address of the struct, not the struct itself.
Watch for structure padding when you write it (for example, it may write more than 50 bytes).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 11th, 2003, 06:08 AM
#4
Thread Starter
<?="Moderator"?>
cheers, could i do the same if i wanted to read the file into a struct?
-
Dec 11th, 2003, 09:15 AM
#5
Frenzied Member
Code:
ifstream in(USER_DATA, ios::in | ios::binary);
in.read((char*)&ud, sizeof(ud));
in.close();
-
Dec 11th, 2003, 10:41 AM
#6
Frenzied Member
FYI:
If you look at the file with a text editor you can't read the text but
if you open it with a binary editor you can understand it.
You may want to look at encrypting the output if you don't want it
to easily be read.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|