开发者

passing structure to a function in C

开发者 https://www.devze.com 2023-02-03 21:52 出处:网络
I have a structure : PROCESSENTRY32 pe32; I want to pass this structure to a function. The function will create a file and write the data in the structure to that file. Name of the function is take

I have a structure :

PROCESSENTRY32 pe32;

I want to pass this structure to a function. The function will create a file and write the data in the structure to that file. Name of the function is takeinput(). I passed the structure to function :

errflag = takeinput (&pe32);

In takeinput(PROCESSENTRY32 *pe31), I cre开发者_StackOverflowated a file D:\File.txt by using createfile(). Now I have to write the date from into file.txt. I am using :

WriteFile( 
                    hFile,           // open file handle
                    DataBuffer,      // start of data to write
                    dwBytesToWrite,  // number of bytes to write
                    &dwBytesWritten, // number of bytes that were written
                    NULL);            // no overlapped structure

Here hFile I know. Last three I know. but I am confused about the DataBuffer paramter. What to pass there ? There are many variables in structure pe31. Can anybody help me in this?

If there is another way to write the data of the structure to the file.txt, kindly explain me. Thanks in advance.


That's the buffer which holds your data. Your call will be:

takeinput (PROCESSENTRY32* ppe32)
{
    WriteFile( 
                hFile,           // open file handle
                (void*)ppe2,      // pointer to buffer to write
                sizeof(PROCESSENTRY32),  // number of bytes to write
                &dwBytesWritten, // this will contain number of bytes actually written
                NULL);            // no overlapped structure

    // some other stuff
}

After return dwBytesWritten should be equal to sizeof(PROCESSENTRY32).


WriteFile function signature is

BOOL WINAPI WriteFile(
__in         HANDLE hFile,
__in         LPCVOID lpBuffer,
__in         DWORD nNumberOfBytesToWrite,
__out_opt    LPDWORD lpNumberOfBytesWritten,
__inout_opt  LPOVERLAPPED lpOverlapped
);

your DataBuffer is lpBuffer in the signature and lpBuffer is a pointer to the buffer containing the data to be written to the file or device. You should explicitly cast a pointer to your data (PROCESSENTRY32 pe31) to a pointer to void ( (void)pe31 ) and pass it to WriteFile.


Have you read the documentation for the WriteFile function? That might help you understand what each of the parameters that it takes are used for and what they mean.

BOOL WINAPI WriteFile(
  __in         HANDLE hFile,
  __in         LPCVOID lpBuffer,
  __in         DWORD nNumberOfBytesToWrite,
  __out_opt    LPDWORD lpNumberOfBytesWritten,
  __inout_opt  LPOVERLAPPED lpOverlapped
);

You say you're confused about the DataBuffer parameter. MSDN explains that this is:

A pointer to the buffer containing the data to be written to the file or device.

This buffer must remain valid for the duration of the write operation. The caller must not use this buffer until the write operation is completed.

So, in essence, the DataBuffer (lpBuffer) parameter is where you provide the data that you want to be written out to the text file.

There's a full example of how to open and write to a file here. You should be able to follow along with the code to see how to code this for your specific case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号