我试图使150字节写在UNBEFFERED模式(测试),但我错过了一些东西.如果有人可以帮助?计算对齐sim是正确的,但仍然有一个错误的数据写入文件上的数据.
#include <Windows.h>
#include <iostream>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <string>
void DisplayError(LPTSTR lpszFunction);
// Macro Given by Microsoft for computing the closest even interval from the progam need.
#define ROUND_UP_SIZE(Value,Pow2) ((SIZE_T) ((((ULONG)(Value)) + (Pow2) - 1) & (~(((LONG)(Pow2)) - 1))))
#define ROUND_UP_PTR(Ptr,Pow2) ((void *) ((((ULONG_PTR)(Ptr)) + (Pow2) - 1) & (~(((LONG_PTR)(Pow2)) - 1))))
// Config structure for Unbeffered IO.
struct HandleConfig {
DWORD SectorsPerCluster;
DWORD BytesPerSector;
DWORD NumberOfFreeClusters;
DWORD TotalNumberOfClusters;
};
void MesureDisk(HandleConfig* config, BOOL debug) {
DWORD lpSectorsPerCluster;
DWORD lpBytesPerSector;
DWORD lpNumberOfFreeClusters;
DWORD lpTotalNumberOfClusters;
if (GetDiskFreeSpaceA(
NULL,
&lpSectorsPerCluster,
&lpBytesPerSector,
&lpNumberOfFreeClusters,
&lpTotalNumberOfClusters)) {
if (!debug) {
std::cout << "Disk measure Ok.\r\n";
}
else {
std::cout << "Disk measure Ok.\r\n";
std::cout << "Disk Structure: " << "\r\n" <<
"SectorsPerCluster: " << lpSectorsPerCluster << "\r\n" <<
"lpBytesPerSector: " << lpBytesPerSector << "\r\n" <<
"lpNumberOfFreeClusters: " << lpNumberOfFreeClusters << "\r\n" <<
"lpTotalNumberOfClusters: " << lpTotalNumberOfClusters << "\r\n";
}
}
config->SectorsPerCluster = lpSectorsPerCluster;
config->BytesPerSector = lpBytesPerSector;
config->NumberOfFreeClusters = lpNumberOfFreeClusters;
config->TotalNumberOfClusters = lpTotalNumberOfClusters;
}
void Windows_write_UNBEFFERED() {
// Config the handle.
HANDLE hFile;
char DataBuffer[] = "Some data for test !\r\n";
DWORD dwBytesToWrite = 150;//(DWORD)strlen(DataBuffer);
DWORD dwBytesWritten = 0;
LPCSTR filePath = "File.txt"; // File PATH.
hFile = CreateFileA(
filePath,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_FLAG_NO_BUFFERING, //Set Flag
NULL
);
// Measure the disc.
HandleConfig fileConf;
MesureDisk(&fileConf, TRUE);//Display the struct on Terminal.
// Ensure you have one more sector than WRITE_SIZE would require.
size_t sizeNeeded = fileConf.BytesPerSector + ROUND_UP_SIZE(dwBytesToWrite, fileConf.BytesPerSector);
char* Buffer = new char[sizeNeeded]; //heap Allocation
void* BufferAligned = ROUND_UP_PTR(Buffer, fileConf.BytesPerSector); //Offset the allocated Pointer from the disk Nomenclature.
printf("sizeneeded: %d \r\n", sizeNeeded);
printf("Allocated pointer address at: %X \r\n",Buffer );
printf("Allocated pointer address Aligned at: %X \r\n", BufferAligned);
// void* BU = VirtualAlloc(NULL, dwBytesToWrite, MEM_COMMIT,PAGE_READWRITE);
if (!WriteFile(
hFile,
BufferAligned,
sizeNeeded,
&dwBytesWritten,
NULL)) {
DWORD message = GetLastError();
DisplayError((LPTSTR)&message);
delete[] Buffer;
CloseHandle(hFile);
return;
}
CloseHandle(hFile);
// VirtualFree(BU, 0, MEM_RELEASE);
delete[] Buffer;
}
int main() {
Windows_write_UNBEFFERED();
return EXIT_SUCCESS;
}
void DisplayError(LPTSTR lpszFunction)
// Routine Description:
// Retrieve and output the system error message for the last-error code
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
lpDisplayBuf =
(LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)
+ lstrlen((LPCTSTR)lpszFunction)
+ 40) // account for format string
* sizeof(TCHAR));
if (FAILED(StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error code %d as follows:\n%s"),
lpszFunction,
dw,
lpMsgBuf)))
{
printf("FATAL ERROR: Unable to output error code.\n");
}
_tprintf(TEXT("ERROR: %s\n"), (LPCTSTR)lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
字符串
我试图写150个字节在UNBEFFERED模式下的窗口与WriteFile()到一个简单的文件,但有些东西是失踪的这段代码编译,但产生一个文件与错误的内容。
1条答案
按热度按时间d5vmydt91#
字符串