gpt4 book ai didi

c - MapViewOfFile-OpenFileMapping(需要帮助)

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:45 24 4
gpt4 key购买 nike

``我正在努力做我的家庭作业,但我无法继续,需要你的帮助。我有两个使用相同文件 (log.txt) 的进程。当我尝试同时写一些东西时,它会覆盖并且只有一个进程写入 txt。

这是我的创作过程,

enter code here
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "..\\def.h"



int main(){

HANDLE hReadPipe;
HANDLE hWritePipe;
HANDLE hMMap;
DWORD bytesWritten;
HANDLE hFile;
STARTUPINFO si_e;
PROCESS_INFORMATION pi_e;

STARTUPINFO si_d;
PROCESS_INFORMATION pi_d;


SECURITY_ATTRIBUTES sa;

sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);

if(!CreatePipe(&hReadPipe,&hWritePipe, &sa,0))
fprintf(stderr,"Unable to create pipe.\n");
ZeroMemory(&si_e, sizeof(STARTUPINFO));
ZeroMemory(&si_d, sizeof(STARTUPINFO));



if((hFile =CreateFile( FILE_NAME,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL))
== INVALID_HANDLE_VALUE){
fprintf(stderr,"PARENT: Unable to open file %s: %d\n",FILE_NAME,GetLastError());
exitPrompt();
}

if((hMMap = CreateFileMapping( hFile,
NULL,
PAGE_READWRITE,
0,
BUFF_SIZE*2,
"Global\\file_log_txt"))
== NULL){
fprintf(stderr,"PARENT: Unable to create memory mapping: %d\n",GetLastError());

}



si_e.cb = sizeof(si_e);
si_e.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si_e.hStdOutput =hWritePipe;
si_e.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si_e.dwFlags = STARTF_USESTDHANDLES;

if(!CreateProcess( NULL,
"..\\Debug\\Encrypter.exe",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si_e,
&pi_e))
fprintf(stderr,"Unable to create child process.\n");








si_d.cb = sizeof(si_d);
si_d.hStdInput= hReadPipe;
si_d.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si_d.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si_d.dwFlags = STARTF_USESTDHANDLES;

if(!CreateProcess( NULL,
"..\\Debug\\Decrypter.exe",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si_d,
&pi_d))
fprintf(stderr,"Unable to create child process.\n");



Sleep(5000000);
return 0;

1.流程:

enter code here
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include "..\\def.h"
int main()
{
int i;
HANDLE writeHandle;
HANDLE hFile, hMMap;
DWORD bytesToWrite,bytesWritten,a=50;
char * pFile, *start;
char message[50][15];
char code[]=" ";
int index;
writeHandle = GetStdHandle(STD_OUTPUT_HANDLE);

if((hMMap = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Global\\file_log_txt")) == NULL){
fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());

}

if( ( pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,BUFF_SIZE)) == NULL)
{
fprintf(stderr,"Unable to create map view: %d\n",GetLastError());

}

//student number =160201007(0+0+7=7) +7encrypter
while(1){

gets(code);
for( i=0;i<strlen(code);i++){
code[i]=code[i]+7;
}
bytesToWrite = strlen(code);
bytesToWrite++;
WriteFile(writeHandle,code,bytesToWrite,&bytesWritten,NULL);

}
start = pFile;
*(pFile)=*(pFile)+50;
while(pFile < start+ 100){
*(pFile=pFile+80);
*(pFile++) = 'A';
break;
}
system("pause");
return 0;
}

2.过程:

enter code here
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include "..\\def.h"
int main()

{

char message[80];
int index,i;
HANDLE hFile, hMMap;
char * pFile, *start;
if((hMMap = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Global\\file_log_txt")) == NULL){
fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());

}

if( ( pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,BUFF_SIZE)) == NULL)
{
fprintf(stderr,"Unable to create map view: %d\n",GetLastError());

}


for(index = 0;(message[index]=getchar()) != 0;index++);
for( i=0;i<strlen(message);i++){
message[i]=message[i]-7;
}
printf("Decrypter : %s\n",message);
start = pFile;
while(pFile < start + 50){

*(pFile++) = 'A';
break;

}
system("pause");
return 0;
}

"..\def.h"

enter code here

#ifndef __DEF_H
#define __DEF_H
#include <Windows.h>
#include <stdio.h>
//#include <stdlib.h>
#define BUFF_SIZE 1024 *64
#define FILE_NAME "log.txt"


void exitPrompt(){
system("pause");
exit(0);
}
#endif

最佳答案

你有很多问题:)

首先,在 Windows 下,内存映射文件无法扩展 - 这使得它们相当不适合写入日志文件。

同一个文件的多个 View 应该是连贯的,只要它们不引用网络位置并且你不混合内存映射和普通文件 I/O,所以应该可以编造一些东西这行得通……但是您需要一些方法来编排您的写入。

恐怕无法绕过某些进程间通信来安全地执行此操作。对于标准的 ASCII 日志文件,肯定没有,对于自定义二进制格式,您可能会使用长度前缀字符串和原子数据访问进行一些欺骗(尽管我不确定在写入内存映射文件时是否可以保证原子性),但要正确地做到这一点将是一件棘手的事情——随着竞争条件的可能性而成熟。

关于c - MapViewOfFile-OpenFileMapping(需要帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8621690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com