How to read and write binary file using readfile and writefile ?

This is a discussion on How to read and write binary file using readfile and writefile ? within the Windows Programming forums, part of the Platform Specific Boards category; How to read and write binary file using readfile and writefile ? Code: int main() { int variable[]={10000}; int var[1]; ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    48

    How to read and write binary file using readfile and writefile ?

    How to read and write binary file using readfile and writefile ?

    Code:
    int main()
    {
        int variable[]={10000};
        int var[1];
        DWORD dwBytesWritten = 0,dwBytesRead=0;
    HANDLE file =    CreateFileA("c:\\friendzia.dat",0x10000000 ,3,0,2,0x80,0);
    WriteFile(file,variable,4,&dwBytesWritten,NULL);
    ReadFile(file,var,4,&dwBytesRead,0);
    cout<<var[0];
    CloseHandle(file);
    getch();
    return 0;
    }
    Please correct my mistake.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Firstly, it's idiotic not to use the symbolic names for the flag parameters to CreateFile.

    Secondly, you either have to reposition the file pointer or close/reopen the file to read the value you just wrote.

    Try
    Code:
    SetFilePointer(file, 0, NULL, FILE_BEGIN);
    between your write and your read.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    Yes, I figured that out, Now I just want to read a binary file and print it to console 4 byte at time .
    kindly help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  2. Testing EOF with WriteFile/ReadFile ...
    By csonx_p in forum Windows Programming
    Replies: 5
    Last Post: 05-26-2008, 09:40 AM
  3. Copy/Read/Write a binary file(.mp3)
    By swapnaoe in forum C Programming
    Replies: 9
    Last Post: 04-22-2008, 02:38 AM
  4. using ReadFile, WriteFile, strSafe
    By talz13 in forum C++ Programming
    Replies: 0
    Last Post: 02-08-2005, 11:37 AM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21