none
how to read and write files RRS feed

  • Question

  • hi, i want to know how to read or write files. i want to be able to save stats on a game instead of having to restart every time. i think text files would work the best, right? anybody know how? ive heard that lots of people do this, but i cant find out how.

    Thank you.

    Saturday, May 22, 2010 10:57 PM

Answers

  • You use the CreateFile function to both open and create new files depending on the parameters you pass. See more here: Creating and Opening Files. This function returns a HANDLE that you use to identity the file in your code. Data is read with the ReadFile API and written using the WriteFile API. When you are done using the file, you must close its handle using the CloseHandle API.

    Here's a sample on how to create a new file:

    HANDLE hFile = CreateFile(strFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    This create a new file in write mode. By changing the parameters you can open an existing file and specify how the function should do if the file already exists or not.

    Writing to the file (which is very similar to the process of reading from it) requires a pointer to a data area containing the information to write. Note that the WriteFile API know nothing about the data you write and treats it as an array of BYTEs:

    DWORD dwWritten;
    
    if(WriteFile(hFile, pBuffer, dwSize, &dwWritten, NULL))
    {
     // Success
    }
    else
    {
     // Failed. See GetLastError()
    }

    The pBuffer variable points to the data you want to write and dwSize contains the size of the data in BYTEs. Upon return the WriteFile API will return TRUE if successful and write the number of written bytes to the dwWritten variable. If the function fails, it returns FALSE and sets the internal error number which you can query by using the GetLastError function.

    Never forget to close your files with CloseHandle. There are more functions to manipulate files: File I/O Functions.


    João Paulo Figueira (Device Application Development MVP)
    • Marked as answer by Davis123 Sunday, May 23, 2010 6:53 PM
    Sunday, May 23, 2010 6:28 PM

All replies

  • Hi, 

     

    You can use CreateFile, ReadFile and WriteFile API.

     

    -Herojit

    Sunday, May 23, 2010 3:43 AM
  • do you have any tutorials because i dont know what to do sorry
    Sunday, May 23, 2010 12:40 PM
  • You use the CreateFile function to both open and create new files depending on the parameters you pass. See more here: Creating and Opening Files. This function returns a HANDLE that you use to identity the file in your code. Data is read with the ReadFile API and written using the WriteFile API. When you are done using the file, you must close its handle using the CloseHandle API.

    Here's a sample on how to create a new file:

    HANDLE hFile = CreateFile(strFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    This create a new file in write mode. By changing the parameters you can open an existing file and specify how the function should do if the file already exists or not.

    Writing to the file (which is very similar to the process of reading from it) requires a pointer to a data area containing the information to write. Note that the WriteFile API know nothing about the data you write and treats it as an array of BYTEs:

    DWORD dwWritten;
    
    if(WriteFile(hFile, pBuffer, dwSize, &dwWritten, NULL))
    {
     // Success
    }
    else
    {
     // Failed. See GetLastError()
    }

    The pBuffer variable points to the data you want to write and dwSize contains the size of the data in BYTEs. Upon return the WriteFile API will return TRUE if successful and write the number of written bytes to the dwWritten variable. If the function fails, it returns FALSE and sets the internal error number which you can query by using the GetLastError function.

    Never forget to close your files with CloseHandle. There are more functions to manipulate files: File I/O Functions.


    João Paulo Figueira (Device Application Development MVP)
    • Marked as answer by Davis123 Sunday, May 23, 2010 6:53 PM
    Sunday, May 23, 2010 6:28 PM
  • thanks i think i got it now.
    Sunday, May 23, 2010 6:53 PM