使用 Windows Api 将字符串写入映射文件
我正在尝试使用 c 和 Visual Studio 将字符串写入映射文件。
( pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,0))
start = pFile;
while(pFile < start + 750){
*(pFile++) = ' ';
*(pFile++) = 'D';
*(pFile++) = 'N';
*(pFile++) = 'C';
*(pFile++) = 'L';
*(pFile++) = 'D';
*(pFile++) = ' ';
if(!((pFile - start) % 50))
*(pFile++) = 10;
else
*(pFile++) = ',';
}
如果我写这样的东西我可以写得很好。但我想在这个文件中写入一个字符串。我该怎么办?我已经尝试过
sprintf(toFile, "A message of %3d bytes is received and decrypted.\n", strlen(message));
WriteFile(pFile,toFile,strlen(toFile),&bytesWritten,NULL);
这个了...
I'm trying to write a string to a mapped file with c and visual studio.
( pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,0))
start = pFile;
while(pFile < start + 750){
*(pFile++) = ' ';
*(pFile++) = 'D';
*(pFile++) = 'N';
*(pFile++) = 'C';
*(pFile++) = 'L';
*(pFile++) = 'D';
*(pFile++) = ' ';
if(!((pFile - start) % 50))
*(pFile++) = 10;
else
*(pFile++) = ',';
}
If i write something like this i can write fine. but i want to write a string this file. How can i do? I have tried
sprintf(toFile, "A message of %3d bytes is received and decrypted.\n", strlen(message));
WriteFile(pFile,toFile,strlen(toFile),&bytesWritten,NULL);
this allready...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WriteFile()
需要一个打开的文件HANDLE
,而不是指向内存地址的指针。只需将新数据直接写入所指向的内存内容即可。您可以使用 C 库字符串函数来实现此目的,例如:WriteFile()
expects an openedHANDLE
to a file, not a pointer to a memory address. Just write your new data directly to the contents of the memory being pointed at. You can use C library string functions for that, eg: