News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

reading & writing entire structures to a disk file

Started by devilhorse, November 25, 2009, 07:25:14 PM

Previous topic - Next topic

devilhorse

I have Googled until my eyes are ooggled. I am trying to find an example or some guidance on how to write/read an entire struct to a disk file. Most of the stuff on the net shows how to rea/write a single member of a struct. Basically trying to do the same as this C code.


struct user
{
   char first_name[30];
   char last_name[30];
   unsigned int age;
   time_t create_time;
};



/* Writing */
void write_data( struct user* data )
{
   int fd = 0;
   /* open the file in append mode */
   fd = open( "data.dat", O_RDWR|O_CREAT|O_APPEND );
   
   /* write the binary structure right to the file */
   write( fd, (struct user*) user, sizeof( struct user ));
   close( fd );
}


void read_data()
{
   int fd = 0;
   struct user data;
   
   /* open the file */
   fd = open( "data.dat", O_RDONLY );
   
   /* read the data into 'data' until we read the end of the file */
   while( read( fd, &data, sizeof( struct user )))
   {
      printf("Just read:\nfirst: %s\nlast\nage: %d\ncreate time: %d\n",
         data.first_name, data.last_name, data.age, data.create_time );
   }

   /* close file */
   close( fd );
}


Tried this and it only gets the first character from each member.


;Global structure of a name and address
mail_struct Struct
m_name Byte 25
m_address Byte 25
m_city Byte 12
m_state Byte 3
m_zipcode Byte 6
m_code Byte 7
mail_struct EndS

;Global file handle
fp HANDLE ?

enter_name Proc
Local item:mail_struct
Local lpWritten:DWord


Invoke GetText, hEditName, item.m_name
Invoke GetText, hEditAddress, item.m_address
Invoke GetText, hEditCity, item.m_city
Invoke GetText, hEditState, item.m_state
Invoke GetText, hEditZipcode, item.m_zipcode
Invoke GetText, hEditCode, item.m_code
Invoke add_to_file, item



Ret
enter_name EndP


add_to_file Proc item:mail_struct
Local lpWritten:DWord


Invoke exist, Addr FILENAME
.If Eax == 0
Invoke MessageBox, NULL, CTXT("Write error"), CTXT("Please check if file exists!"), MB_ICONEXCLAMATION
Return 0
.EndIf

Invoke CreateFile, Addr FILENAME, GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
Mov fp, Eax
Invoke SetFilePointer, fp, 0, 0, FILE_END
Invoke WriteFile, fp, item, SizeOf item, Addr lpWritten, NULL
Invoke CloseHandle, fp

Ret
add_to_file EndP


added code tags

MichaelW

eschew obfuscation

devilhorse

The GetText procedure  copies the text from a Win32 edit control and stores it in a buffer. In this case (Invoke GetText, hEditName, item.m_name) i am getting the text from an edit control whose handle is hEditName and copying that text to the struct member variable named m_name. If I call MessageBox function and pass m_name as one of the parameters the text shows up just fine.

jj2007

Quote from: devilhorse on November 25, 2009, 07:25:14 PM
Tried this and it only gets the first character from each member.
Might be a Unicode problem, although it's difficult to judge if you don't see the full code. Try displaying with MessageBoxW....

MichaelW

QuoteThe GetText procedure copies the text from a Win32 edit control and stores it in a buffer. In this case (Invoke GetText, hEditName, item.m_name) i am getting the text from an edit control whose handle is hEditName and copying that text to the struct member variable named m_name.

I can deduce all of that from your code. What I cannot be sure of is what exactly you mean by "Tried this and it only gets the first character from each member". If you mean that GetText is only copying the first character, then the problem is that you are passing it only the first character. Try passing the address of the members instead, but note that if GetText is able to copy a single character when passed a single character, then it will need to be modified to handle a pointer.


eschew obfuscation

cobold

Quote from: devilhorse on November 25, 2009, 07:25:14 PM



Invoke CreateFile, Addr FILENAME, GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
Mov fp, Eax
Invoke SetFilePointer, fp, 0, 0, FILE_END
Invoke WriteFile, fp, item, SizeOf item, Addr lpWritten, NULL
---> Invoke WriteFile, fp, ADDR item ...... (function expects an address)???
Invoke CloseHandle, fp

Ret
add_to_file EndP


hth

added code tags


0x401000

I have another question, can we like that, during the work program saves all settings in the program body, to store change and delete? That is all the information is always within the program?!

thomas_remkus


rags

with:

add_to_file Proc item:mail_struct

you are attempting to pass a mail_struc struct as a parameter.
Just as cobold said it should be a POINTER to a mail_struct

also are you using MASM?
IF so the struct declaration should be:

mail_struct STRUCT
         m_name     BYTE    25 DUP (?)
         m_address  BYTE    25  DUP (?)
         m_city       BYTE    12   DUP (?)
         m_state     BYTE    3    DUP (?)
         m_zipcode  BYTE    6    DUP (?)
         m_code     BYTE    7     DUP (?)
mail_struct ENDS


with this:
m_name Byte 25
the struct member m_name is being created with a size of 1 byte, and an initial value of 25,
just as the rest of the members are being created as BYTE sized members and being initialized to the value
after the byte.
The 'x DUP (?)', tells masm to create x number of bytes of UNINITIALIZED space for a member.

you could use code like this to pass the pointer to item:

     lea eax, item                        ;<------- load eax with the address of item
     Invoke add_to_file, eax

I hope this helps.
God made Man, but the monkey applied the glue -DEVO

hutch--

1. Get ADDRESS of structure.
2. Get LENGTH of structure
3. Open or create a file
4. Write LENGTH bytes from STRUCTURE ADDRESS to file

go back to (4.) for each structure until all are written to disk.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

devilhorse

Thanks to ALL. Yes rags I am using masm. And I see my mistake thanks to you.   Hutch I follow your advice too. "A man's errors are his portals of discovery."