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 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <windows.h>
#include <stdio.h>
#define SIZE 256
int factorial( int num )
{
if ( num == 0 )
return 1;
else
return num * factorial( num-1 );
}
// returns the nth catalan numbers
int catalan( int n )
{
return( factorial( 2 * n ) / ( factorial( n + 1 ) * factorial( n ) ) );
}
int main( int argc, char *argv[] )
{
HANDLE hFile;
HANDLE hMapFile;
LPVOID mapAddress;
char series[SIZE];
char buf[SIZE];
int i;
int seriesSize = 0;
seriesSize = atoi( argv [ 1 ] );
// first create/open the file
hFile = CreateFile( "temp.txt", // this is my error //
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
if( hFile == INVALID_HANDLE_VALUE )
{
fprintf( stderr,"Could not open file temp.txt (%d).\n",GetLastError() );
return -1;
}
// now obtain a mapping for it
hMapFile = CreateFileMapping( hFile,
NULL,
PAGE_READWRITE,
0,
0,
TEXT("SharedObject") );
if( hMapFile == NULL )
{
fprintf(stderr,"Could not create mapping (%d).\n", GetLastError());
return -1;
}
// now establish a mapped viewing of the file
mapAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
if( mapAddress == NULL )
{
printf( "Could not map view of file (%d).\n", GetLastError() );
return -1;
}
// write to shared memory
for( i = 1; i <= seriesSize; i++ )
{
sprintf( buf, "%d, ", catalan(i) );
strcat( series, buf );
//printf( "**%s**", buf);
}
sprintf((char *)mapAddress, "%s\n", series);
//sprintf((char *)mapAddress,"%s","Shared memory message");
while (1);
// remove the file mapping
UnmapViewOfFile(mapAddress);
// close all handles
CloseHandle(hMapFile);
CloseHandle(hFile);
}
|