• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 803
  • Last Modified:

Write a struct to disk using WriteFile API

Greetings,
I am stuck trying to write a struct on disk using the WriteFile API function. The function works for dumping simple strings on disk by not a struct.

Example:

struct Record{
  CHAR cName[30];
  CHAR cZip[10];
}myRecord;

void main(){
   DWORD dwBytes;
   BOOL bResult;
   HANDLE hFile1 = CreateFile("d:\\a.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
   bResult = WriteFile(hFile1, myRecord, sizeof(myRecord), &dwBytes, NULL);
...       
}

Is there other way to accomplish this task?
0
misha051797
Asked:
misha051797
  • 4
  • 2
1 Solution
 
jkrCommented:
Well, that's already the right way. But you'd better use the address of myRecord, e.g. '&myRecord' in order to get it working. The difference is, when you e.g. declare
char acMyArray[ 100];
'acMyArray' is already a pointer ro the first byte od 'acMyArray', whereas
'myRecord' in the example above is the record 'by value'. So the line should read
   bResult = WriteFile(hFile1, &myRecord, sizeof(struct Record), &dwBytes, NULL);

0
 
misha051797Author Commented:
jkr thank you. i guess the proposed method should work. does the readfile API accepts the address as a second paprameter as well?

Here is what I have in mind:

struct Record{
      CHAR cName[30];
      CHAR cCity[10];
};

int main(){

      Record myRecord;

      DWORD dwBytes;
      BOOL bResult;
      
      cout << "Enter your name: ";
      cin >> myRecord.cName;
      cout << "Enter your city: ";
      cin >> myRecord.cCity;
      
        HANDLE hFile1 = CreateFile("d:\\a.txt", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
      
      
      
    bResult = WriteFile(hFile1, &myRecord, sizeof(myRecord), &dwBytes, NULL);
   

bResult = ReadFile(hFile1, &myRecord, sizeof(myRecord), &dwBytes, NULL);

// Now after i read the data back in is it automatically placed in the appropriate structure data elements (cName and cCity)?
Can i use following to retrieve the cName?:


      cout << myRecord.cName;

      SetEndOfFile(hFile1);

      return TRUE;
}
Thank you.
0
 
jkrCommented:
No - if you decide to write the data using 'WriteFile()' (which writes it in binary form!), you'll have to use 'ReadFile()' to retrieve it (BTW: What you're actually doing is called 'fixed record length' storage). So, if you use 'ReadFile()', the data will be stored in the appropriate fields of the structure. (BTW: The 2nd parameter is declared as a 'void*', so you can pass in _anything_ - if it is not a valid memory buffer, the result is - as you have experienced - inpredictable...)
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
misha051797Author Commented:
So in this case my program should work. Right? As you can see I did use WriteFile() finction. But when I read data into myRecord struct I get garbage. What is wrong with the example above?
Thank you.
0
 
jkrCommented:
Well, 'sizeof(myRecord)' seems a bit unusual - i assume if you examine the contents of 'dwBytes', it'll already be different from 40. You'd better use 'sizeof(struct Record)' as i stated above. 'sizeof(<instance>)' usually is used for arrays...
0
 
alexoCommented:
The problem is the file pointer.  At the end of the WriteFile operation, the file pointer points to *beyond* the written data.  And that's where you read from.

You should use SetFilePointer() to reposition the file pointer.

From the docs:

The WriteFile function writes data to a file and is designed for both synchronous and asynchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished.

The ReadFile function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read, unless the file handle is created with the overlapped attribute. If the file handle is created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the read operation.

0
 
jkrCommented:
Well, misha - i assume you already closed the file before reading from it, haven't you? If not, alexo is right and you should use 'SetFilePointer( hFile1, 0, NULL, FILE_BEGIN);' to reposition it to the start of the file...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now