2011-01-10 74 views
0

我有一个结构:传递结构的功能用C

PROCESSENTRY32 pe32; 

我想这个结构传递给函数。该函数将创建一个文件并将结构中的数据写入该文件。函数的名称是takeinput()。我通过结构功能:

errflag = takeinput (&pe32);

在takeinput(PROCESSENTRY32 * pe31)中,我使用createfile()创建了一个文件D:\ File.txt。现在我必须将日期写入file.txt。我正在使用:

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 

这里h文件我知道。后三个我知道。但我对DataBuffer参数感到困惑。什么通过那里?结构pe31中有许多变量。有人可以帮我吗?

如果还有另一种方法将结构的数据写入file.txt,请给我解释一下。提前致谢。

+0

为什么你的文件名是file.txt,你想用二进制格式还是文本写数据? – honibis 2011-01-10 09:41:54

回答

2

这是保存您的数据的缓冲区。您的通话将被:

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 
} 

回报dwBytesWritten后应等于sizeof(PROCESSENTRY32)

0

WriteFile函数签名是

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

您的DataBuffer是lpBuffer在签名和lpBuffer是一个指针,指向包含数据要被写入到该文件或装置中的缓冲器。您应该明确地将一个指向您的数据(PROCESSENTRY32 pe31)的指针指向void((void)pe31)并将其传递给WriteFile。

0

你看了documentationWriteFile函数吗?这可能会帮助您了解每个参数所用的参数以及它们的含义。

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

你说你对DataBuffer参数感到困惑。 MSDN解释这是:

指向包含要写入文件或设备的数据的缓冲区的指针。

该缓冲区在写入操作期间必须保持有效。写操作完成之前,调用方不得使用此缓冲区。

因此,在本质上,DataBufferlpBuffer)的参数是你提供你想要写出到文本文件中的数据。

有一个如何打开和写入文件here的完整示例。你应该能够跟随代码一起看看如何为你的特定情况编码。