1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#ifdef _WIN32
#include "windows.h"
#include <Fcntl.h>
#endif
#define MY_TEST_SIZE 1000000
int main(void)
{
char * l_buffer = (char*)malloc(MY_TEST_SIZE*sizeof(char));
memset(l_buffer,0,MY_TEST_SIZE);
strcpy(l_buffer,"Hello World\nIl fait beau aujourdhui!\n#EOF\nYoupi!\nWindows ca me gave carrement, tout est simple sous Linux alors que sous Windows le moindre truc est super complique et mal document!!!e\n");
char l_read_buffer[11];
for(uint32_t l_index = 0 ;l_index < 100 ; ++l_index)
{
#ifdef _WIN32
HANDLE l_file_handle = CreateFile("toto",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_ALWAYS,/*FILE_ATTRIBUTE_NORMAL*/FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,NULL);
printf("l_file_handle = %d\n",l_file_handle);
if(INVALID_HANDLE_VALUE == l_file_handle)
{
printf("ERROR when opening temporary file\n");
exit(-1);
}
HANDLE l_handle = CreateFileMapping(l_file_handle/*INVALID_HANDLE_VALUE*/,0,PAGE_READWRITE,0,MY_TEST_SIZE,NULL);
if(INVALID_HANDLE_VALUE == l_handle)
{
printf("ERROR when opening file mapping\n");
exit(-1);
}
LPVOID l_mapped_memory = MapViewOfFile(l_handle,FILE_MAP_WRITE,0,0,0);
if(l_mapped_memory == NULL)
{
printf("Error when mapping file in memory\n");
exit(-1);
}
memcpy(l_mapped_memory,l_buffer,MY_TEST_SIZE);
printf("l_handle = %d\n",l_handle);
// Get the file descriptor from the handle
int fd = _open_osfhandle((long)l_file_handle, O_RDONLY);
printf("File descriptor = %d\n",fd);
// Get the handle from the file descriptor
FILE * l_file = _fdopen(fd,"r");
#else
FILE * l_file = fmemopen(l_buffer,MY_TEST_SIZE,"r");
#endif
if(NULL == l_file)
{
printf("Unable to perform open");
exit(-1);
}
while(!feof(l_file))
{
size_t l_read_size = fread(l_read_buffer,11,1,l_file);
write(1,l_read_buffer,11);
}
#ifdef _WIN32
UnmapViewOfFile(l_mapped_memory);
CloseHandle(l_handle);
CloseHandle(l_file_handle);
#else
fclose(l_file);
#endif
}
free(l_read_buffer);
free(l_buffer);
} |
Partager