作成ファイルの任意のサイズにWindowsをお使いのC++API
-
13-10-2019 - |
質問
を作っていきたいファイルの任意のサイズのブックボタンをクリックC/C++のAPIとなります。私はWindows XPのサービスパック2 32ビットの仮想アドレスのメモリ空間です。わかる方がいたら、CreateFile.
しかし、CreateFileいませんのでサイズarument、その理由をしたいのpassサイズ引数として作成メモリマッピングファイルをすることによりユーザがアクセスデータ構造を所定のサイズです。きせの際お申し出くださいので、適切なWindowsのC/C++のAPI機能としてのファイルを作成しのarbritrary所定の大きさはどれくらいですか?ご
解決
UNIXでこれを行うには、(必要なファイル化-1)を求めて、バイトを書きます。バイトの値は何でもかまいませんが、ゼロは明らかな選択です。
他のヒント
君 CreateFile
いつものように、 SetFilePointerEx
目的のサイズまで、そして電話してください SetEndOfFile
.
ファイルは必要ありません。MSDNから、メモリマップされたファイルのバッキングとしてPageFileを使用できます CreateFileMapping
機能ページ:
hfileがnivalid_handle_valueの場合、呼び出しプロセスは、dwmaximumsizehighおよびdwmaximumsizelowパラメーターのファイルマッピングオブジェクトのサイズも指定する必要があります。このシナリオでは、CreateFileMappingは、ファイルシステム内のファイルではなく、システムページングファイルによってバックされる指定されたサイズのファイルマッピングオブジェクトを作成します。
マッピングオブジェクトを使用して共有できます DuplicateHandle
.
あなたのコメントによると、実際にクロスプラットフォームソリューションが必要なので、確認してください インタープロセスをブーストします 図書館。クロスプラットフォームの共有メモリ設備などを提供します
Linuxでこれを行うには、次のことを行うことができます。
/**
* Clear the umask permissions so we
* have full control of the file creation (see man umask on Linux)
*/
mode_t origMask = umask(0);
int fd = open("/tmp/file_name",
O_RDWR, 00666);
umask(origMask);
if (fd < 0)
{
perror("open fd failed");
return;
}
if (ftruncate(fd, size) == 0)
{
int result = lseek(data->shmmStatsDataFd, size - 1, SEEK_SET);
if (result == -1)
{
perror("lseek fd failed");
close(fd);
return ;
}
/* Something needs to be written at the end of the file to
* have the file actually have the new size.
* Just writing an empty string at the current file position will do.
*newDataSize
* Note:
* - The current position in the file is at the end of the stretched
* file due to the call to lseek().
* - An empty string is actually a single '\0' character, so a zero-byte
* will be written at the last byte of the file.
*/
result = data->write(fd, "", 1);
if (result != 1)
{
perror("write fd failed");
close(fd);
return;
}
}