Advertisement

WriteFile (API Question)

Started by July 25, 2000 05:47 PM
3 comments, last by cyberben 24 years, 5 months ago
Hi there, I''m just learning to use file funcitons under a Windows environment and don''t understand all the parameters in the API calls. Both ReadFile and WriteFile have an argument for the number of bytes to write/read and a pointer to the number of bytes written/read to the file? What do they mean by this? I''ve been using examples from Chris Hobbs Tutorial for loading (In assembler) and he moves esp (Stack Pointer Register) to eax and then use that for the pointer of the number of bytes thing!?!? I''m confused, any help? Thanks, Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Or does anyone know of some good tutorials on File I/O that DON''T use the C and C++ builtin file stuff or IRQ''s. Just plain API.

That''d be even better!
- Ben

__________________________
Mencken's Law:
"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."
- Popular Mechanics, forecasting the relentless march of science in 1949
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Advertisement
Say you want to copy data from one file into another (and I will assume both files are already open):

        HFILE hSrc; // handle to already opened source fileHFILE dest; // handle to already opened destination fileLPVOID buffer = NULL; // memory location to store file dataDWORD dwReadBytes = 0; // number of bytes to readDWORD dwWriteBytes = 0; // number of bytes to writeDWORD dwBytesWritten = 0; // number of bytes written// first, get the size of the source filedwReadBytes = GetFileSize (hSrc,NULL);// now allocate memory for the data - the docs recommend using// VirtualAlloc for memory that will be used with the Windows// file routines (instead of malloc/calloc/new)VirtualAlloc (buffer,dwReadBytes,MEM_COMMIT,PAGE_READWRITE);// now open the file. We want to store the number of bytes read// in the dwWriteBytes variable since we'll be writing the data // to another fileif (!ReadFile (hSrc,buffer,dwReadBytes,&dwWriteBytes,NULL)){   // error handling code here}// dwWriteBytes should now be equal to dwReadBytes. If not,// then the entire file was not read into the bufferif (dwWriteBytes != dwReadBytes){    // error handling code here}// if all is well, write the data in the buffer out to the// destination file - the total # of bytes written will be // stored in the dwBytesWritten varWriteFile (hFile,buffer,dwWriteBytes,&dwBytesWritten,NULL);// dwBytesWritten should be equal to dwWriteBytesif (dwBytesWritten != dwWriteBytes){   // error handling code here}        


HTH

Edited by - Aldacron on July 25, 2000 9:41:48 PM
Thanks! I figured it out between your post and some logical thinking... I was also using the OPEN_EXISTING flag which will fail if the file isn''t already there! So I changed it to CREATE_ALWAYS which always overwrites or creates a new file, works great!! Thanks again!
See ya,
Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Actually if you look here, go check out my post on what people think is necassary in a map file under Isometric GAme Programming section! Thanks - Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949

This topic is closed to new replies.

Advertisement